Skip to content

Creating your first agent

Open In Colab

any-agent uses the python asyncio module to support async functionality. When running in Jupyter notebooks, this means we need to enable the use of nested event loops. We’ll install any-agent and enable this below using nest_asyncio.

%pip install 'any-agent' --quiet
%pip install ddgs --quiet
import warnings
import nest_asyncio
# Suppress technical warnings to reduce noise for the user
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)
nest_asyncio.apply()

For this tutorial, we’ll use Mistral’s mistral-small-latest (fast and affordable). You could also use:

  • gpt-4o-mini
  • claude-3-5-sonnet-latest
  • Any other model supported by any-agent

We’ll use search_web(DuckDuckGo), which provides Duck Duck Go search for free with no API key required.

import os
from getpass import getpass
if "MISTRAL_API_KEY" not in os.environ:
os.environ["MISTRAL_API_KEY"] = getpass("Enter your Mistral API Key: ")
from any_agent import AgentConfig, AnyAgent
from any_agent.tools import search_web
# We use the 'mistral-small-latest' model we promised in the text
agent = AnyAgent.create(
"tinyagent",
AgentConfig(model_id="mistral:mistral-small-latest", tools=[search_web]),
)

Now we’ve configured our agent, so it’s time to run it! Let’s give it a simple task: find 5 trending new TV shows that were released recently.

agent_trace = agent.run(
"What are 5 tv shows that are trending in 2025? Check a few sites, and provide the name of the show, the exact release date, the genre, and a brief description of the show."
)

The agent.run method returns an AgentTrace object, which has a few convenient attributes for displaying some interesting information about the run.

print(agent_trace.final_output) # Final answer
print(f"Duration: {agent_trace.duration.total_seconds():.2f} seconds")
print(f"Usage: {agent_trace.tokens.total_tokens:,}")
print(f"Cost (USD): {agent_trace.cost.total_cost:.6f}")