# Tracing

An [AgentTrace](https://docs.mozilla.ai/any-agent/api-reference/tracing) is returned when calling `agent.run` or `agent.run_async`.

`any-agent` generates standardized (regarding the structure) [OpenTelemetry](https://opentelemetry.io/) traces regardless of the framework used, based on the [Semantic conventions for generative AI systems](https://opentelemetry.io/docs/specs/semconv/gen-ai/). This means that any OpenTelemetry exporter compatible with the Python SDK can be added. More information can be found [below](#adding-an-opentelemetry-exporter).

{% hint style="info" %}
Check the exposed attributes in [GenAI](https://docs.mozilla.ai/any-agent/api-reference/tracing)
{% endhint %}

You can try to find the subtle differences (regarding the content) across frameworks in the examples below.

{% hint style="success" %}
The following are real traces generated by executing [one of our integration tests](https://github.com/mozilla-ai/any-agent/blob/main/tests/integration/test_load_and_run_agent.py).
{% endhint %}

## Console

Here is what the console output looks like:

#### AGNO

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/AGNO_trace.html)

#### GOOGLE

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/GOOGLE_trace.html)

#### LANGCHAIN

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/LANGCHAIN_trace.html)

#### LLAMA\_INDEX

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/LLAMA_INDEX_trace.html)

#### OPENAI

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/OPENAI_trace.html)

#### SMOLAGENTS

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/SMOLAGENTS_trace.html)

#### TINYAGENT

[Interactive view](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/TINYAGENT_trace.html)

{% hint style="success" %}
The spans are printed to the console by default, using the `callback` mechanism.

See [Default Callbacks](https://docs.mozilla.ai/any-agent/agents/callbacks#default-callbacks) for more information and how to disable this behavior.
{% endhint %}

## Spans

Here's what the returned `agent_trace.spans` look like when [dumped to JSON format](#dumping-to-file):

#### AGNO

You can download the [AGNO trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/AGNO_trace.json).

#### GOOGLE

You can download the [GOOGLE trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/GOOGLE_trace.json).

#### LANGCHAIN

You can download the [LANGCHAIN trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/LANGCHAIN_trace.json).

#### LLAMA\_INDEX

You can download the [LLAMA\_INDEX trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/LLAMA_INDEX_trace.json).

#### OPENAI

You can download the [OPENAI trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/OPENAI_trace.json).

#### SMOLAGENTS

You can download the [SMOLAGENTS trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/SMOLAGENTS_trace.json).

#### TINYAGENT

You can download the [TINYAGENT trace JSON](https://github.com/mozilla-ai/any-agent/blob/gitbook-docs/traces/TINYAGENT_trace.json).

## Dumping to File

The AgentTrace object is a pydantic model and can be saved to disk via standard pydantic practices:

```python
from any_agent import AgentConfig, AnyAgent
from any_agent.tools import search_web

agent = AnyAgent.create(
    "openai",
    agent_config=AgentConfig(
            model_id="mistral:mistral-small-latest",
            tools=[search_web],
    )
)
agent_trace = agent.run("Which agent framework is the best?")
with open("agent_trace.json", "w", encoding="utf-8") as f:
  f.write(agent_trace.model_dump_json(indent=2, serialize_as_any=True))
```

{% hint style="success" %}
Passing `serialize_as_any=True` makes sure that the `final_output` gets dumped even when `AgentConfig.output_type` is used.
{% endhint %}

## Adding an OpenTelemetry exporter

Before starting to use the library, you can add new OpenTelemetry exporters and processors as needed.

The following code will use the OpenTelemetry Python SDK to send the agent traces to an additional endpoint using OTLP over HTTP in the indicated URL:

{% hint style="success" %}
To use custom exporters you need to install their required dependencies, In this example, it would be `pip install opentelemetry-exporter-otlp`.
{% endhint %}

```python
from opentelemetry.trace import get_tracer_provider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter

tp = get_tracer_provider()
http_exporter = OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")
tp.add_span_processor(SimpleSpanProcessor(http_exporter))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mozilla.ai/any-agent/core-concepts/tracing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
