How Context Propagation Works
When a request crosses a service boundary, the calling service injects its trace context into outgoing HTTP headers. The receiving service extracts that context and attaches all of its spans to the same trace. The result is a single unified session in HoneyHive, even though the work happened in different processes. HoneyHive provides two helpers that handle the plumbing:| Helper | Where | What it does |
|---|---|---|
inject_context_into_carrier(headers, tracer) | Client side | Injects trace ID, session ID, and project into HTTP headers |
with_distributed_trace_context(headers, tracer) | Server side | Extracts context from headers and attaches it to all spans in the block |
What You’ll Build
A distributed AI agent architecture with a client orchestrator calling both a remote and a local agent:Prerequisites
- Python 3.11+
- HoneyHive API key from https://app.honeyhive.ai
- Google Gemini API key from https://aistudio.google.com/apikey
Installation
Step 1: Set Environment Variables
Step 2: Create the Agent Server (Remote Service)
The agent server runs a Google ADK research agent. The key line iswith_distributed_trace_context(), which extracts the incoming trace context from HTTP headers and attaches it to all spans created inside the block.
Create agent_server.py:
agent_server.py
agent_server.py
with_distributed_trace_context() handles extracting the trace ID, session ID, and project from the incoming headers, attaching context so all spans link to the caller’s trace, and cleaning up when the block exits (even on exceptions).
Step 3: Create the Client Application
The client orchestrates both remote and local agent calls. For the remote call, it usesinject_context_into_carrier() to propagate trace context via HTTP headers.
Create client_app.py:
client_app.py
client_app.py
inject_context_into_carrier() adds the W3C traceparent header plus HoneyHive baggage (session ID, project, source) to your outgoing HTTP headers.
Step 4: Run and Test
Run the Client Application (in a separate terminal)
View in HoneyHive
Go to https://app.honeyhive.ai, open the Spans from
distributed-tracing-tutorial project, and click Traces. You’ll see a unified trace hierarchy:agent-server appear as children of client-app spans, even though they ran in different processes.Troubleshooting
| Problem | Solution |
|---|---|
| Remote spans don’t appear | Ensure inject_context_into_carrier() is called before the HTTP request, and the server uses with_distributed_trace_context() |
| Connection refused | Start the agent server first |
Missing GOOGLE_API_KEY | Export it in your environment |
| Different projects in dashboard | Both server and client must use the same project in HoneyHiveTracer.init() |

