Creating your first agent¶
If you're looking to build your first agent using a few simple tools, this is a great place to start. In this cookbook example, we will create and run a simple agent that has access to a few web tools. This can be easily expanded to add more advanced tools and features. 🚀
Install Dependencies¶
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'
import nest_asyncio
nest_asyncio.apply()
Configure the Agent¶
Now it's time to configure the agent! At this stage you have a few choices:
Pick the framework¶
We support a variety of underlying agent frameworks (OpenAI, Smolagents, Langchain, TinyAgent, etc), which all have their own particular agentic AI implementations. For this tutorial's simple use case, any of the frameworks should work just fine, but any-agent makes it easy to try out a different framework later, if we so choose. For this example, we will use the TinyAgent framework.
Pick an LLM¶
Regardless of which agent framework you choose, each framework supports LiteLLM, which is a proxy that allows us to use whichever LLM inside the framework, hosted on by any provider. For example, we could use a local model via llama.cpp or llamafile, a google hosted gemini model, or a AWS bedrock hosted Llama model. For this example, let's use OpenAI's gpt-4.1-nano.
Pick which tools to use¶
In this tutorial, we will provide the agent with access to web searches and web page visits. In later examples we will show more advanced tool usage like Model Context Protocol (MCP) tool use or inter-agent communication using Agent-2-Agent (A2A).
import os
from getpass import getpass
if "OPENAI_API_KEY" not in os.environ:
print("OPENAI_API_KEY not found in environment!")
api_key = getpass("Please enter your OPENAI_API_KEY: ")
os.environ["OPENAI_API_KEY"] = api_key
print("OPENAI_API_KEY set for this session!")
else:
print("OPENAI_API_KEY found in environment.")
OPENAI_API_KEY found in environment.
from any_agent import AgentConfig, AnyAgent
from any_agent.tools import search_web, visit_webpage
agent = AnyAgent.create(
"tinyagent", # See all options in https://mozilla-ai.github.io/any-agent/
AgentConfig(model_id="gpt-4.1-mini", tools=[search_web, visit_webpage]),
)
Run the Agent¶
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? Please provide the name of the show, the exact release date, the genre, and a brief description of the show."
)
╭──────────────────────────────────────────── CALL_LLM: gpt-4.1-mini ─────────────────────────────────────────────╮ │ ╭─ INPUT ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ [ │ │ │ │ { │ │ │ │ "role": "system", │ │ │ │ "content": "You are an agent - please keep going until the user's query is completely resolved, before │ │ │ │ }, │ │ │ │ { │ │ │ │ "role": "user", │ │ │ │ "content": "What are 5 tv shows that are trending in 2025? Please provide the name of the show, the exa │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ [ │ │ │ │ { │ │ │ │ "tool.name": "search_web", │ │ │ │ "tool.args": "{\"query\":\"trending TV shows 2025\"}" │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 333, │ │ │ │ "output_tokens": 20, │ │ │ │ "input_cost": 0.00013319999999999999, │ │ │ │ "output_cost": 3.2e-05 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── EXECUTE_TOOL: search_web ────────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "query": "trending TV shows 2025" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ Output ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ "[Best TV Shows of 2025: New Series to Watch Now](https://editorial.rottentomatoes.com/guide/best-new-tv-se │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── CALL_LLM: gpt-4.1-mini ─────────────────────────────────────────────╮ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ [ │ │ │ │ { │ │ │ │ "tool.name": "visit_webpage", │ │ │ │ "tool.args": "{\"url\": \"https://editorial.rottentomatoes.com/guide/best-new-tv-series-shows/\"}" │ │ │ │ }, │ │ │ │ { │ │ │ │ "tool.name": "visit_webpage", │ │ │ │ "tool.args": "{\"url\": \"https://www.polygon.com/tv/493214/best-new-tv-2025-release-calendar-schedule- │ │ │ │ }, │ │ │ │ { │ │ │ │ "tool.name": "visit_webpage", │ │ │ │ "tool.args": "{\"url\": \"https://www.timeout.com/film/must-see-tv-shows-for-2025-you-cant-miss\"}" │ │ │ │ }, │ │ │ │ { │ │ │ │ "tool.name": "visit_webpage", │ │ │ │ "tool.args": "{\"url\": \"https://www.hollywoodreporter.com/lists/most-anticipated-tv-shows-2025/\"}" │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 1191, │ │ │ │ "output_tokens": 161, │ │ │ │ "input_cost": 0.0004764, │ │ │ │ "output_cost": 0.0002576 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── EXECUTE_TOOL: visit_webpage ──────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "url": "https://editorial.rottentomatoes.com/guide/best-new-tv-series-shows/" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ Output ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ "Best TV Shows of 2025: New Series to Watch Now | Rotten Tomatoes\n \n[Home](https://www.rottentomatoes.com │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── EXECUTE_TOOL: visit_webpage ──────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "url": "https://www.polygon.com/tv/493214/best-new-tv-2025-release-calendar-schedule-shows-series" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ Output ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ "The 50 big new TV shows to watch in 2025 | Polygon[Go to the footer](#sit │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── EXECUTE_TOOL: visit_webpage ──────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "url": "https://www.hollywoodreporter.com/lists/most-anticipated-tv-shows-2025/" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ Output ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ "Most Anticipated TV Shows of 2025\n \n \n \n \n \n # 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}")
Here are 5 trending TV shows in 2025: 1. Poker Face: Season 2 - Release Date: May 2025 - Genre: Mystery, Crime, Drama - Description: The second season doubles down on the winning virtues of its predecessor with Natasha Lyonne and others in a jackpot of mysteries. 2. Common Side Effects: Season 1 - Release Date: 2025 - Genre: Animated, Comedy, Drama - Description: An addictive mix of expressive animation, heady concepts, and an offbeat sense of humor. 3. Blue Eye Samurai - Release Date: May 21, 2025 - Genre: Animated, Action, Adventure - Description: A swordcrafting story that pushes the idea into new territory. 4. The Wheel of Time: Season 3 - Release Date: 2025 (exact TBD) - Genre: Fantasy, Adventure, Drama - Description: Season 3 is the series’ highest-rated, continuing the epic fantasy saga. 5. Black Mirror: Season 7 - Release Date: 2025 (exact TBD) - Genre: Sci-Fi, Thriller, Drama - Description: The new season features guest stars and presents the first sequel to the popular USS Callister episode. Let me know if you want more info or other recommendations! Duration: 21.08 seconds Usage: 31,149 Cost (USD): 0.013451