Context¶
The coalex_context() function creates a parent span that tags all child spans with agent metadata, making them queryable in the dashboard.
Usage¶
What Context Propagates¶
| Attribute | Description |
|---|---|
agent_id |
The agent that owns this request |
request_id |
Unique identifier for the request (links traces to evaluations) |
version |
Agent version string (optional, for A/B testing) |
These attributes are attached as span attributes to every child span, enabling you to:
- Filter traces by agent in the dashboard
- Link evaluations to their corresponding traces via
request_id - Compare versions side-by-side in metrics views
Best Practices¶
- One context per user request — Create a new
coalex_context()for each incoming request to your agent. - Use unique request IDs — Use UUIDs or your application's request ID to avoid collisions.
- Reuse the same request ID in
evaluate()— Therequest_idincoalex_context()must match the one inevaluate()to link traces and evaluations. - Set version for A/B tests — When testing different prompt versions, set
versionto differentiate traces.
Nesting¶
Coalex contexts can be nested. Inner contexts create child spans of the outer context:
with coalex.coalex_context(agent_id="orchestrator", request_id="req-001"):
# Orchestrator span
with coalex.coalex_context(agent_id="sub-agent", request_id="req-001"):
# Sub-agent span (child of orchestrator)
response = client.chat.completions.create(...)
SDK Reference¶
coalex_context()— Full API reference