Caching and observability

Motleycrew provides a universal caching engine that caches LLM and tool calls, as well as other web requests out of the box.
It works with all requests made using most popular Python HTTP clients: requests, HTTPX, and Curl CFFI.
We also provide integration with Lunary, an open-source observability platform.
To enable tracking via Lunary, you need to set the LUNARY_PUBLIC_KEY environment variable.

We will demonstrate the tracking and caching capabilities on a simple task with one agent and one tool.

[ ]:
import os
os.environ["OPENAI_API_KEY"] = "<Your OpenAI API key here>"
os.environ["LUNARY_PUBLIC_KEY"] = "<Your Lunary public key here (get it in Lunary settings)>"
[ ]:
# Alternatively, load everything from .env
from dotenv import load_dotenv
load_dotenv()
[2]:
from motleycrew import MotleyCrew
from motleycrew.agents.langchain.react import ReActMotleyAgent
from motleycrew.tasks import SimpleTask
from langchain_community.tools import DuckDuckGoSearchRun
[3]:
from motleycache import enable_cache
enable_cache()  # Caching is on!
[4]:
crew = MotleyCrew()

duckduckgo_search = DuckDuckGoSearchRun()

writer = ReActMotleyAgent(
    name="writer",
    prompt_prefix="Using the results of a web search, write an article on the latest advancements in AI in 2024.",
    tools=[duckduckgo_search],
)

task = SimpleTask(
    crew=crew,
    agent=writer,
    name="write an article on the latest advancements in AI",
    description="Using the results of a web search, write an article on the latest advancements in AI in 2024. "
                "Write in a way that speaks to the general audience."
)
crew.run()
WARNING:root:Multithreading is not implemented yet, will run in single thread
WARNING:root:No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string
WARNING:root:No known Cypher type matching annotation typing.List[str], will use JSON string
WARNING:root:No known Cypher type matching annotation typing.List[str], will use JSON string
WARNING:root:No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string
[4]:
[TaskUnit(status=done)]

In this example, we’ll be looking at the trace view in Lunary interface, which looks like this:

image.png

The trace view in Lunary is very useful for looking at what’s going on inside your agent.
In our case it should look something like this: