Skip to content

Callbacks

any_agent.callbacks.base.Callback

Base class for AnyAgent callbacks.

Source code in src/any_agent/callbacks/base.py
class Callback:
    """Base class for AnyAgent callbacks."""

    def before_agent_invocation(self, context: Context, *args, **kwargs) -> Context:
        """Will be called before the Agent invocation starts."""
        return context

    def before_llm_call(self, context: Context, *args, **kwargs) -> Context:
        """Will be called before any LLM Call starts."""
        return context

    def before_tool_execution(self, context: Context, *args, **kwargs) -> Context:
        """Will be called before any Tool Execution starts."""
        return context

    def after_agent_invocation(self, context: Context, *args, **kwargs) -> Context:
        """Will be called once the Agent invocation ends."""
        return context

    def after_llm_call(self, context: Context, *args, **kwargs) -> Context:
        """Will be called after any LLM Call is completed."""
        return context

    def after_tool_execution(self, context: Context, *args, **kwargs) -> Context:
        """Will be called after any Tool Execution is completed."""
        return context

after_agent_invocation(context, *args, **kwargs)

Will be called once the Agent invocation ends.

Source code in src/any_agent/callbacks/base.py
def after_agent_invocation(self, context: Context, *args, **kwargs) -> Context:
    """Will be called once the Agent invocation ends."""
    return context

after_llm_call(context, *args, **kwargs)

Will be called after any LLM Call is completed.

Source code in src/any_agent/callbacks/base.py
def after_llm_call(self, context: Context, *args, **kwargs) -> Context:
    """Will be called after any LLM Call is completed."""
    return context

after_tool_execution(context, *args, **kwargs)

Will be called after any Tool Execution is completed.

Source code in src/any_agent/callbacks/base.py
def after_tool_execution(self, context: Context, *args, **kwargs) -> Context:
    """Will be called after any Tool Execution is completed."""
    return context

before_agent_invocation(context, *args, **kwargs)

Will be called before the Agent invocation starts.

Source code in src/any_agent/callbacks/base.py
def before_agent_invocation(self, context: Context, *args, **kwargs) -> Context:
    """Will be called before the Agent invocation starts."""
    return context

before_llm_call(context, *args, **kwargs)

Will be called before any LLM Call starts.

Source code in src/any_agent/callbacks/base.py
def before_llm_call(self, context: Context, *args, **kwargs) -> Context:
    """Will be called before any LLM Call starts."""
    return context

before_tool_execution(context, *args, **kwargs)

Will be called before any Tool Execution starts.

Source code in src/any_agent/callbacks/base.py
def before_tool_execution(self, context: Context, *args, **kwargs) -> Context:
    """Will be called before any Tool Execution starts."""
    return context

any_agent.callbacks.context.Context dataclass

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.

Source code in src/any_agent/callbacks/context.py
@dataclass
class 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.
    """

    current_span: Span
    """You can use the span in your callbacks to get information consistently across frameworks.

    You can find information about the attributes (available under `current_span.attributes`) in
    [Attributes Reference](./tracing.md#any_agent.tracing.attributes).
    """

    trace: AgentTrace
    tracer: Tracer

    shared: dict[str, Any]
    """Can be used to store arbitrary information for sharing across callbacks."""

current_span instance-attribute

You can use the span in your callbacks to get information consistently across frameworks.

You can find information about the attributes (available under current_span.attributes) in Attributes Reference.

shared instance-attribute

Can be used to store arbitrary information for sharing across callbacks.

any_agent.callbacks.span_cost.AddCostInfo

Bases: Callback

Add cost information to the LLM Call spans.

Extend the LLM Call span attributes with 2 new keys
  • gen_ai.usage.input_cost
  • gen_ai.usage.output_cost
Source code in src/any_agent/callbacks/span_cost.py
class AddCostInfo(Callback):
    """Add cost information to the LLM Call spans.

    Extend the LLM Call span attributes with 2 new keys:
        - gen_ai.usage.input_cost
        - gen_ai.usage.output_cost
    """

    def after_llm_call(self, context: Context, *args, **kwargs):
        span = context.current_span
        add_cost_info(span)
        return context

any_agent.callbacks.span_print.ConsolePrintSpan

Bases: Callback

Use rich's console to print the Context.current_span.

Source code in src/any_agent/callbacks/span_print.py
class ConsolePrintSpan(Callback):
    """Use rich's console to print the `Context.current_span`."""

    def __init__(self, console: Console | None = None) -> None:
        """Init the ConsolePrintSpan.

        Args:
            console: An optional instance of `rich.console.Console`.
                If `None`, a new instance will be used.

        """
        self.console = console or Console()

    def after_llm_call(self, context: Context, *args, **kwargs) -> Context:
        span = context.current_span

        operation_name = span.attributes.get(GenAI.OPERATION_NAME, "")

        if operation_name != "call_llm":
            return context

        panels = []

        if messages := span.attributes.get(GenAI.INPUT_MESSAGES):
            panels.append(
                Panel(JSON(messages), title="INPUT", style="white", title_align="left")
            )

        if output_panel := _get_output_panel(span):
            panels.append(output_panel)

        if usage := {
            k.replace("gen_ai.usage.", ""): v
            for k, v in span.attributes.items()
            if "usage" in k
        }:
            panels.append(
                Panel(
                    JSON(json.dumps(usage)),
                    title="USAGE",
                    style="white",
                    title_align="left",
                )
            )

        self.console.print(
            Panel(
                Group(*panels),
                title=f"{operation_name.upper()}: {span.attributes.get(GenAI.REQUEST_MODEL)}",
                style="yellow",
            )
        )

        return context

    def after_tool_execution(self, context: Context, *args, **kwargs) -> Context:
        span = context.current_span

        operation_name = span.attributes.get(GenAI.OPERATION_NAME, "")

        if operation_name != "execute_tool":
            return context

        panels = [
            Panel(
                JSON(span.attributes.get(GenAI.TOOL_ARGS, "{}")),
                title="Input",
                style="white",
                title_align="left",
            )
        ]

        if output_panel := _get_output_panel(span):
            panels.append(output_panel)

        self.console.print(
            Panel(
                Group(*panels),
                title=f"{operation_name.upper()}: {span.attributes.get(GenAI.TOOL_NAME)}",
                style="blue",
            )
        )
        return context

__init__(console=None)

Init the ConsolePrintSpan.

Parameters:

Name Type Description Default
console Console | None

An optional instance of rich.console.Console. If None, a new instance will be used.

None
Source code in src/any_agent/callbacks/span_print.py
def __init__(self, console: Console | None = None) -> None:
    """Init the ConsolePrintSpan.

    Args:
        console: An optional instance of `rich.console.Console`.
            If `None`, a new instance will be used.

    """
    self.console = console or Console()

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.

Returns:

Type Description
list[Callback]

A list of instances containing:

Source code in src/any_agent/callbacks/__init__.py
def get_default_callbacks() -> list[Callback]:
    """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].

    Returns:
        A list of instances containing:

            - [`AddCostInfo`][any_agent.callbacks.span_cost.AddCostInfo]
            - [`ConsolePrintSpan`][any_agent.callbacks.span_print.ConsolePrintSpan]

    """
    return [AddCostInfo(), ConsolePrintSpan()]