callbacks

A set of lifecycle hooks that run before or after an agent, language-model request, or tool execution. Hooks are small pieces of code used to observe, validate, limit, or extend those steps.

In plain words
What is it for?
Use it to inspect prompts and responses, count tokens, authorize tool calls, collect metrics, validate results, and clean up after an agent run.
Why use it?
It lets you add logging, checks, rate limits, cost tracking, and error handling without changing the agent’s core code.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/mozilla-ai/any-agent/callbacks
Clone the repo
git clone --depth 1 https://github.com/mozilla-ai/any-agent
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,988 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02988
Opus 5 $0.00000 $0.01494
Sonnet 5 $0.00000 $0.00598
Haiku 4.5 $0.00000 $0.00299

Measured yesterday against content hash baaa7d2c11be, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

callbacks scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

docs/agents/callbacks.md · 389 lines

How it starts

The opening of the file, as written. The whole thing — 389 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Agent Callbacks

Callbacks provide hooks into the lifecycle of an AnyAgent execution. Using callbacks, you can monitor, control, and extend agent behavior without modifying the core underlying agent logic.

Implementing Callbacks

All callbacks must inherit from the base Callback class and can choose to implement any subset of the available callback methods. These methods include:

Callback Method When It Fires Example Use Cases
before_agent_invocation Once at start, before any LLM calls Initialize counters, validate inputs, set up logging
before_llm_call Before each LLM API call Content filtering, cost tracking, prompt inspection
after_llm_call After LLM responds, before adding to history Response validation, token counting, logging
before_tool_execution Before each tool runs Rate limiting, input validation, authorization checks
after_tool_execution After tool completes Result validation, metrics collection, error handling
after_agent_invocation Once at end, before returning final response Cleanup, final metrics, audit logging
# Minimum valid implementation
def before_llm_call(self, context: Context, *args, **kwargs) -> Context:
    return context  # <--- Essential!

Managing State (Context)

During an agent run (agent.run_async or agent.run), a unique Context object is created and shared across all callbacks.

Use Context.shared (a dictionary) to persist data across different steps and callbacks.

Note: The Context object is mutable. You should modify Context.shared directly and return the same object.

any-agent populates the Context.current_span property so that callbacks can access information in a framework-agnostic way.

You can see what attributes are available for LLM Calls and Tool Executions by examining the GenAI class.

Common Pattern: Initialize a counter in one callback and check it in another.

Read the full file on GitHub · 389 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 389 lines · 0 tokens per session scan A baaa7d2c11be

Subscribe to this mod's changes

callbacks is an agent published in the GitHub repository mozilla-ai/any-agent (1,197 stars, last pushed 1mo ago), licensed Apache-2.0. It costs nothing until one of its globs matches a file; then it loads 2,988 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.