Skip to content

Callbacks

Base class for AnyAgent callbacks.

Base class for AnyAgent callbacks. Subclass and override any subset of the lifecycle methods.

Will be called before the Agent invocation starts.

def before_agent_invocation(
self,
context: Context,
*args,
**kwargs,
) -> Context

Will be called before any LLM Call starts.

def before_llm_call(
self,
context: Context,
*args,
**kwargs,
) -> Context

Will be called after any LLM Call is completed.

def after_llm_call(
self,
context: Context,
*args,
**kwargs,
) -> Context

Will be called before any Tool Execution starts.

def before_tool_execution(
self,
context: Context,
*args,
**kwargs,
) -> Context

Will be called after any Tool Execution is completed.

def after_tool_execution(
self,
context: Context,
*args,
**kwargs,
) -> Context

Will be called once the Agent invocation ends.

def after_agent_invocation(
self,
context: Context,
*args,
**kwargs,
) -> Context

Object that will be shared across callbacks. Each AnyAgent.run has a separate Context available. shared can be used to store and pass information across different callbacks.

Shared context object passed through all callbacks during an agent run.

FieldTypeDescription
current_spanSpanThe active OpenTelemetry span with attributes (see GenAI)
traceAgentTraceCurrent execution trace
tracerTracerOpenTelemetry tracer instance
shareddict[str, Any]Arbitrary shared state across callbacks

Default callback that prints span information to the console using Rich formatting.


any_agent.callbacks.get_default_callbacks()

Section titled “any_agent.callbacks.get_default_callbacks()”

Return instances of the default callbacks used in any-agent. This function is called internally when the user doesn’t provide a value for [AgentConfig.callbacks][any_agent.config.AgentConfig.callbacks].

def get_default_callbacks() -> list[Callback]

Returns: A list of instances containing: - [ConsolePrintSpan][any_agent.callbacks.span_print.ConsolePrintSpan]