Using Callbacks¶
This cookbook illustrates how to implement and use custom callbacks in any-agent
.
You can find more information about callbacks in the docs:
%pip install 'any-agent'
import nest_asyncio
nest_asyncio.apply()
/home/daavoo/any-agent/.venv/bin/python: No module named pip Note: you may need to restart the kernel to use updated packages.
import os
from getpass import getpass
for key in ("MISTRAL_API_KEY", "TAVILY_API_KEY"):
if key not in os.environ:
print(f"{key} not found in environment!")
api_key = getpass(f"Please enter your {key}: ")
os.environ[key] = api_key
print(f"{key} set for this session!")
else:
print(f"{key} found in environment.")
MISTRAL_API_KEY found in environment. TAVILY_API_KEY found in environment.
from any_agent import AgentConfig, AnyAgent
from any_agent.tools import search_tavily
/home/daavoo/any-agent/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
Running with default callbacks¶
any-agent
comes with a set of default callbacks that will be used by default (if you don't pass a value to AgentConfig.callbacks
):
agent = AnyAgent.create(
"tinyagent",
AgentConfig(model_id="mistral/mistral-small-latest", tools=[search_tavily]),
)
agent_trace = agent.run("What are 5 LLM agent frameworks that are trending in 2025?")
╭──────────────────────────────────── CALL_LLM: mistral/mistral-small-latest ─────────────────────────────────────╮ │ ╭─ 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 LLM agent frameworks that are trending in 2025?" │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ [ │ │ │ │ { │ │ │ │ "tool.name": "search_tavily", │ │ │ │ "tool.args": "{\"query\": \"5 LLM agent frameworks that are trending in 2025\"}" │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 335, │ │ │ │ "output_tokens": 27, │ │ │ │ "input_cost": 3.35e-05, │ │ │ │ "output_cost": 8.1e-06 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── EXECUTE_TOOL: search_tavily ──────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "query": "5 LLM agent frameworks that are trending in 2025" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ The Top 5 LLM Frameworks in 2025 - Skillcrush Frameworks are a collection of libraries, tools, and features │ │ │ │ that make it easier for developers to build applications that use large language models (LLMs). Huggingface │ │ │ │ Transformers also makes it easier to use their models with other machine learning — specifically deep │ │ │ │ learning — libraries like TensorFlow and Pytorch. Speaking of TensorFlow…TensorFlow is Google’s open-source │ │ │ │ framework that AI engineers use to build machine learning applications and develop learning models and │ │ │ │ algorithms (artificial neural networks). To be honest, TensorFlow comes with a pretty steep learning curve, │ │ │ │ but its value is unbeatable when working with LLMs. It’s a lot easier to use TensorFlow if you have a basic │ │ │ │ understanding of machine learning, so make sure you check out our beginner’s guide to machine learning. │ │ │ │ │ │ │ │ Top AI Agent Frameworks in 2025: LangChain, AutoGen, CrewAI ... Agentic AI #3 — Top AI Agent Frameworks in │ │ │ │ 2025: LangChain, AutoGen, CrewAI & Beyond | by Aman Raghuvanshi | May, 2025 | Medium Agentic AI #3 — Top AI │ │ │ │ Agent Frameworks in 2025: LangChain, AutoGen, CrewAI & Beyond Whether building a single intelligent │ │ │ │ assistant or deploying a network of collaborating agents, this guide will help you evaluate the tools that │ │ │ │ can take your AI projects to the next level. AI agents are more than just LLM wrappers — they require │ │ │ │ memory, reasoning, decision-making, tool usage, and often complex multi-step workflows. AutoGen, developed │ │ │ │ by Microsoft, is a multi-agent framework built around conversational AI and collaborative workflows. Image │ │ │ │ 25: Building a Multi-Agent AI System with LangChain │ │ │ │ │ │ │ │ Best AI Agent Frameworks in 2025: A Comprehensive Guide - Reddit Image 1: r/AI_Agents icon Go to AI_Agents │ │ │ │ r/AI_Agents Image 2: r/AI_Agents iconr/AI_Agents A place for discussion around the use of AI Agents and │ │ │ │ related tools. AI Agents are LLMs that have the ability to "use tools" or "execute functions" in an │ │ │ │ autonomous or semi-autonomous (also known as human-in-the-loop) fashion. Best AI Agent Frameworks in 2025: │ │ │ │ A Comprehensive Guide As we dive into 2025, the world of AI agent frameworks continues to expand and │ │ │ │ evolve, offering exciting new tools and capabilities for developers and researchers. * Pros: Strong │ │ │ │ integration with Microsoft tools * Features: Adaptive agent creation, LLM integration * Features: │ │ │ │ Visual AI tools, Azure integration * Use Cases: Streamlined AI processes * Multi-agent orchestration is │ │ │ │ key as AI applications grow more complex. │ │ │ │ │ │ │ │ LLM Agent Frameworks 2025: Guide & Comparison - Chatbase Explore LLM agent frameworks in 2025. Understand │ │ │ │ core components, compare LangChain vs AutoGen vs CrewAI, and build agents easily with our │ │ │ │ │ │ │ │ 10 Best AI Agent Frameworks: Picking the Right One | 2025 - Lindy Lindy is a no-code AI agent framework │ │ │ │ designed for business users who want to build autonomous agents. Rasa is an open-source framework for │ │ │ │ building conversational AI agents â including chatbots, voice assistants, and AI-powered helpdesk tools. │ │ │ │ Yet, with the sheer number of agentic and conversational AI frameworks, there is a tool designed for your │ │ │ │ specific needs. An AI agent framework is a toolkit developers use to build autonomous agents. It includes │ │ │ │ components like LLMs, memory, APIs, and orchestration logic. No, you donât. No-code frameworks like Lindy │ │ │ │ enable you to create agents using visual tools and templates. ### Is Lindy an AI agent framework? Yes, │ │ │ │ Lindy is an AI agent framework designed for non-developers. │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────── CALL_LLM: mistral/mistral-small-latest ─────────────────────────────────────╮ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ Here are 5 LLM agent frameworks that are trending in 2025: │ │ │ │ │ │ │ │ 1 Huggingface Transformers: This framework makes it easier to use models with other machine learning │ │ │ │ libraries like TensorFlow and Pytorch. It is a collection of libraries, tools, and features that │ │ │ │ simplify the development of applications using large language models (LLMs) [REF]h7cQdytcT[/REF]. │ │ │ │ 2 TensorFlow: Developed by Google, TensorFlow is an open-source framework used by AI engineers to build │ │ │ │ machine learning applications and develop learning models and algorithms. It has a steep learning curve │ │ │ │ but is highly valuable when working with LLMs [REF]h7cQdytcT[/REF]. │ │ │ │ 3 AutoGen: Developed by Microsoft, AutoGen is a multi-agent framework built around conversational AI and │ │ │ │ collaborative workflows. It is known for its strong integration with Microsoft tools and features like │ │ │ │ adaptive agent creation and LLM integration [REF]h7cQdytcT,h7cQdytcT[/REF]. │ │ │ │ 4 LangChain: This framework is designed for building a single intelligent assistant or deploying a network │ │ │ │ of collaborating agents. It includes components like LLMs, memory, APIs, and orchestration logic │ │ │ │ [REF]h7cQdytcT,h7cQdytcT[/REF]. │ │ │ │ 5 Lindy: Lindy is a no-code AI agent framework designed for business users who want to build autonomous │ │ │ │ agents. It enables users to create agents using visual tools and templates, making it accessible for │ │ │ │ non-developers [REF]h7cQdytcT[/REF]. │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 1352, │ │ │ │ "output_tokens": 348, │ │ │ │ "input_cost": 0.0001352, │ │ │ │ "output_cost": 0.00010439999999999999 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Adding Callback to offload sensitive information¶
Some inputs and/or outputs in your traces might contain sensitive information that you don't want to be exposed in the traces.
We are going to implement a callback that takes the value of the input messages (which contain the instructions and the user prompt), writes it to an external destination (in this example, a local file), and replaces the value of the attribute in the span with a reference to that external destination.
import json
from pathlib import Path
from any_agent.callbacks.base import Callback
from any_agent.callbacks.context import Context
class SensitiveDataOffloader(Callback):
def __init__(self, output_dir: str) -> None:
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True, parents=True)
def before_llm_call(self, context: Context, *args, **kwargs) -> Context:
span = context.current_span
if input_messages := span.attributes.get("gen_ai.input.messages"):
output_file = self.output_dir / f"{span.get_span_context().trace_id}.txt"
output_file.write_text(str(input_messages))
span.set_attribute(
"gen_ai.input.messages", json.dumps({"ref": str(output_file)})
)
return context
We can now provide our callback to the agent.
You can find more information in:
https://mozilla-ai.github.io/any-agent/agents/callbacks/#providing-your-own-callbacks
from any_agent.callbacks import get_default_callbacks
agent = AnyAgent.create(
"tinyagent",
AgentConfig(
model_id="mistral/mistral-small-latest",
tools=[search_tavily],
callbacks=[SensitiveDataOffloader("sensitive-info"), *get_default_callbacks()],
),
)
agent_trace = agent.run("What are 5 LLM agent frameworks that are trending in 2025?")
╭──────────────────────────────────── CALL_LLM: mistral/mistral-small-latest ─────────────────────────────────────╮ │ ╭─ INPUT ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "ref": "sensitive-info/320624889481214829213442967250469732873.txt" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ [ │ │ │ │ { │ │ │ │ "tool.name": "search_tavily", │ │ │ │ "tool.args": "{\"query\": \"5 LLM agent frameworks that are trending in 2025\"}" │ │ │ │ } │ │ │ │ ] │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 335, │ │ │ │ "output_tokens": 27, │ │ │ │ "input_cost": 3.35e-05, │ │ │ │ "output_cost": 8.1e-06 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── EXECUTE_TOOL: search_tavily ──────────────────────────────────────────╮ │ ╭─ Input ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "query": "5 LLM agent frameworks that are trending in 2025" │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ The Top 5 LLM Frameworks in 2025 - Skillcrush Frameworks are a collection of libraries, tools, and features │ │ │ │ that make it easier for developers to build applications that use large language models (LLMs). Huggingface │ │ │ │ Transformers also makes it easier to use their models with other machine learning — specifically deep │ │ │ │ learning — libraries like TensorFlow and Pytorch. Speaking of TensorFlow…TensorFlow is Google’s open-source │ │ │ │ framework that AI engineers use to build machine learning applications and develop learning models and │ │ │ │ algorithms (artificial neural networks). To be honest, TensorFlow comes with a pretty steep learning curve, │ │ │ │ but its value is unbeatable when working with LLMs. It’s a lot easier to use TensorFlow if you have a basic │ │ │ │ understanding of machine learning, so make sure you check out our beginner’s guide to machine learning. │ │ │ │ │ │ │ │ Top AI Agent Frameworks in 2025: LangChain, AutoGen, CrewAI ... Agentic AI #3 — Top AI Agent Frameworks in │ │ │ │ 2025: LangChain, AutoGen, CrewAI & Beyond | by Aman Raghuvanshi | May, 2025 | Medium Agentic AI #3 — Top AI │ │ │ │ Agent Frameworks in 2025: LangChain, AutoGen, CrewAI & Beyond Whether building a single intelligent │ │ │ │ assistant or deploying a network of collaborating agents, this guide will help you evaluate the tools that │ │ │ │ can take your AI projects to the next level. AI agents are more than just LLM wrappers — they require │ │ │ │ memory, reasoning, decision-making, tool usage, and often complex multi-step workflows. AutoGen, developed │ │ │ │ by Microsoft, is a multi-agent framework built around conversational AI and collaborative workflows. Image │ │ │ │ 25: Building a Multi-Agent AI System with LangChain │ │ │ │ │ │ │ │ Best AI Agent Frameworks in 2025: A Comprehensive Guide - Reddit Image 1: r/AI_Agents icon Go to AI_Agents │ │ │ │ r/AI_Agents Image 2: r/AI_Agents iconr/AI_Agents A place for discussion around the use of AI Agents and │ │ │ │ related tools. AI Agents are LLMs that have the ability to "use tools" or "execute functions" in an │ │ │ │ autonomous or semi-autonomous (also known as human-in-the-loop) fashion. Best AI Agent Frameworks in 2025: │ │ │ │ A Comprehensive Guide As we dive into 2025, the world of AI agent frameworks continues to expand and │ │ │ │ evolve, offering exciting new tools and capabilities for developers and researchers. * Pros: Strong │ │ │ │ integration with Microsoft tools * Features: Adaptive agent creation, LLM integration * Features: │ │ │ │ Visual AI tools, Azure integration * Use Cases: Streamlined AI processes * Multi-agent orchestration is │ │ │ │ key as AI applications grow more complex. │ │ │ │ │ │ │ │ LLM Agent Frameworks 2025: Guide & Comparison - Chatbase Explore LLM agent frameworks in 2025. Understand │ │ │ │ core components, compare LangChain vs AutoGen vs CrewAI, and build agents easily with our │ │ │ │ │ │ │ │ 10 Best AI Agent Frameworks: Picking the Right One | 2025 - Lindy Lindy is a no-code AI agent framework │ │ │ │ designed for business users who want to build autonomous agents. Rasa is an open-source framework for │ │ │ │ building conversational AI agents â including chatbots, voice assistants, and AI-powered helpdesk tools. │ │ │ │ Yet, with the sheer number of agentic and conversational AI frameworks, there is a tool designed for your │ │ │ │ specific needs. An AI agent framework is a toolkit developers use to build autonomous agents. It includes │ │ │ │ components like LLMs, memory, APIs, and orchestration logic. No, you donât. No-code frameworks like Lindy │ │ │ │ enable you to create agents using visual tools and templates. ### Is Lindy an AI agent framework? Yes, │ │ │ │ Lindy is an AI agent framework designed for non-developers. │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────── CALL_LLM: mistral/mistral-small-latest ─────────────────────────────────────╮ │ ╭─ OUTPUT ────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ Here are 5 LLM agent frameworks that are trending in 2025: │ │ │ │ │ │ │ │ 1 Huggingface Transformers: Huggingface Transformers is a popular framework that simplifies the use of │ │ │ │ large language models (LLMs) and integrates well with other machine learning libraries like TensorFlow │ │ │ │ and PyTorch. │ │ │ │ 2 TensorFlow: Developed by Google, TensorFlow is an open-source framework widely used for building machine │ │ │ │ learning applications and developing learning models and algorithms. It is particularly valuable when │ │ │ │ working with LLMs, although it has a steep learning curve. │ │ │ │ 3 AutoGen: Developed by Microsoft, AutoGen is a multi-agent framework focused on conversational AI and │ │ │ │ collaborative workflows. It is designed to handle complex multi-step workflows and integrates well with │ │ │ │ Microsoft tools. │ │ │ │ 4 LangChain: LangChain is another prominent framework that supports the creation of intelligent assistants │ │ │ │ and networks of collaborating agents. It is known for its strong integration with various AI tools and │ │ │ │ services. │ │ │ │ 5 CrewAI: CrewAI is a framework that emphasizes multi-agent orchestration, making it ideal for │ │ │ │ applications that require multiple agents to work together. It is particularly useful as AI applications │ │ │ │ grow more complex. │ │ │ │ │ │ │ │ These frameworks provide a range of features and capabilities, making them suitable for different types of │ │ │ │ AI projects and use cases. │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─ USAGE ─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ { │ │ │ │ "input_tokens": 1352, │ │ │ │ "output_tokens": 268, │ │ │ │ "input_cost": 0.0001352, │ │ │ │ "output_cost": 8.04e-05 │ │ │ │ } │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
As you can see in the console output, the input messages in the trace have been now replaced by a reference to the external destination.