Trace AI Agents with OpenObserve and OpenTelemetry
Here's an agent failure that no log will catch. Every request returns 200 OK. The agent is simply calling the same search tool in a loop, re-reading its own output and burning tokens all night. Nothing fails, so nothing alerts, and you find out from the invoice.
More logs won't help here. You need traces: one timeline per run, showing every model call, every tool call, how long each took and how many tokens it used. Here's how to get that with OpenObserve and OpenTelemetry, on a server you control, in about an hour.
Why OpenObserve
OpenObserve is an open-source observability platform for logs, metrics and traces, under the AGPL-3.0 license. It's written in Rust, runs as a single binary, and stores data as Parquet files on disk or S3-compatible storage. The project claims up to 140x lower storage cost than Elasticsearch. I haven't benchmarked that, but columnar storage for repetitive span data is a sensible design.
The current stable release is v1.1.2 (October 20), with 1.2 in release candidates. It also speaks OTLP natively, so anything instrumented with OpenTelemetry can send to it with no extra collector in between.
Step 1: Get Your Endpoint and Credentials
OpenObserve accepts OTLP over HTTP on port 5080, per organization:
https://openobserve.example.com/api/default/v1/traces
default is the organization name. Authentication is HTTP Basic: base64(email:password). In the UI, Data Sources > Traces shows the exact endpoint and header for your organization, ready to copy.
Don't send telemetry as the root user. Create a separate user for ingestion, so you can rotate its password without locking yourself out.
Step 2: Instrument a Python Agent
The OpenTelemetry project maintains an official OpenAI instrumentation that turns every model call into a span with the model name and token counts, following the GenAI semantic conventions. Install it with the SDK and exporter:
pip install opentelemetry-distro opentelemetry-exporter-otlp \
opentelemetry-instrumentation-openai-v2
Point it at OpenObserve with environment variables. Header values are URL-encoded, so the space after Basic becomes %20:
export OTEL_SERVICE_NAME=support-agent
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=https://openobserve.example.com/api/default
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic%20$(echo -n 'agent@example.com:PASSWORD' | base64),stream-name=default"
opentelemetry-instrument python agent.py
Use the base endpoint without /v1/traces here. The exporter adds the path for each signal itself.
Model calls are now traced automatically. Tool calls aren't, and they're usually where loops hide. Wrap each tool in a span:
from opentelemetry import trace
tracer = trace.get_tracer("support-agent")
def search_docs(query: str) -> list[str]:
with tracer.start_as_current_span("tool.search_docs") as span:
span.set_attribute("tool.query", query)
results = run_search(query)
span.set_attribute("tool.result_count", len(results))
return results
Run the agent once, open Traces in OpenObserve, and you'll see each run as a tree: the model call, the tool it chose, the next model call, and so on. A loop looks exactly like what it is: the same span repeated twenty times.
Step 3: Turn Spans into Token Spend
Traces are stored in a stream you can query with SQL. OpenObserve replaces dots in attribute names with underscores, so gen_ai.usage.input_tokens becomes gen_ai_usage_input_tokens. Check the stream's schema in the UI to confirm the names, then:
SELECT gen_ai_request_model AS model,
COUNT(*) AS calls,
SUM(gen_ai_usage_input_tokens) AS input_tokens,
SUM(gen_ai_usage_output_tokens) AS output_tokens
FROM "default"
WHERE gen_ai_request_model IS NOT NULL
GROUP BY model
ORDER BY output_tokens DESC
Standard spans carry token counts, not money. Multiply by your provider's prices in the query or on a dashboard panel, and update them when the price list changes.
Then save the query as a scheduled alert, for example when output tokens in the last hour pass a threshold. That's the alert that catches the looping agent from the start of this article.
Prompts Are Data, Treat Them That Way
By default, the OpenAI instrumentation records metadata, not the text of prompts and responses. You can turn content capture on with OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true, and it's very useful when debugging. But prompts often contain customer messages, documents and secrets pasted by users. Enable it in staging, think hard before enabling it in production, and set a retention period with ZO_COMPACT_DATA_RETENTION_DAYS. Self-hosting at least means that data stays on your server.
OpenObserve or Langfuse?
We've covered Langfuse before, and the two overlap less than you'd think:
| If you need | Better fit |
|---|---|
| Prompt management, evaluations, datasets | Langfuse |
| Agent traces next to your API, database and server logs | OpenObserve |
| SQL queries and alerts over any span attribute | OpenObserve |
| A UI built for LLM engineers reviewing outputs | Langfuse |
If your agent is one part of a larger system, seeing its spans in the same place as the rest of your infrastructure is the real win. When a run is slow, you can tell straight away whether the model or your database was the problem.
Running It on Elestio
OpenObserve on Elestio starts at $16/month for the VM, with SSL, backups, monitoring and updates handled. Your instance gets a public HTTPS address, which is what your agents need to send OTLP from anywhere. Watch disk usage in the first weeks and set retention before it surprises you.
Troubleshooting
401 Unauthorized. The organization is missing from the URL, or the header is wrong. Check that the space after Basic is encoded as %20, and that you used echo -n so no newline ended up in the base64.
404 Not Found. Remove any trailing slash from the endpoint. If you set OTEL_EXPORTER_OTLP_TRACES_ENDPOINT instead, it's used as given, so it must end with /v1/traces.
No spans at all. The app wasn't started with opentelemetry-instrument, or the openai-v2 package isn't installed in the same environment. Spans are sent in batches, so a process killed before a normal exit can lose its last ones.
Token counts are empty for streaming calls. With streaming, the OpenAI API only returns usage when you pass stream_options={"include_usage": True}.
Spans show no prompts. That's the default. See content capture above.
Thanks for reading ❤️ See you in the next one 👋