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_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_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_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_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
    """Always contain the current span.

    The span attributes are defined following OpenTelemetry's [Semantic Conventions for Generative AI](https://opentelemetry.io/docs/specs/semconv/gen-ai/)

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

    trace: AgentTrace
    tracer: Tracer

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

current_span instance-attribute

Always contain the current span.

The span attributes are defined following OpenTelemetry's Semantic Conventions for Generative AI

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

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("gen_ai.operation.name", "")

        if operation_name != "call_llm":
            return context

        panels = []

        if messages := span.attributes.get("gen_ai.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('gen_ai.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("gen_ai.operation.name", "")

        if operation_name != "execute_tool":
            return context

        panels = [
            Panel(
                JSON(span.attributes.get("gen_ai.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('gen_ai.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()]