Query Trace Data with the Agenta API
The Agenta API lets you query trace and span data programmatically. You can filter by attributes, time ranges, and status codes. The API returns data in two formats: flat spans or hierarchical trace trees.
API Endpoints
Two endpoints, same query body — choose the shape you want back:
POST /api/spans/query # returns flat spans
POST /api/traces/query # returns trace trees
Send queries as JSON in the request body. This approach works best for complex filters with nested conditions.
Authentication
Include your API key in the request header:
Authorization: ApiKey YOUR_API_KEY
You can create API keys from the Settings page in your Agenta workspace.
Response Format Options
Spans vs traces
Spans (POST /api/spans/query): Returns individual spans in a flat list. Use this when you need span-level details.
Example response:
{
"count": 2,
"spans": [
{
"trace_id": "abc123",
"span_id": "span001",
"parent_id": "root",
"span_name": "openai.chat",
"status_code": "STATUS_CODE_OK",
"attributes": {
"ag": {
"metrics": {
"costs": { "cumulative": { "total": 0.0023 } },
"tokens": { "cumulative": { "total": 450 } }
}
}
}
},
{
"trace_id": "def456",
"span_id": "span002",
"parent_id": "root",
"span_name": "generate_response",
"status_code": "STATUS_CODE_OK",
"attributes": { "..." }
}
]
}
Traces (POST /api/traces/query): Returns complete traces as hierarchical trees. Each trace groups its spans by trace_id. Use this when you need to see the full request flow.
Example response:
{
"count": 1,
"traces": {
"abc123": {
"spans": {
"generate_response": {
"span_id": "root",
"span_name": "generate_response",
"status_code": "STATUS_CODE_OK",
"spans": {
"openai.chat": {
"span_id": "span001",
"span_name": "openai.chat",
"parent_id": "root",
"status_code": "STATUS_CODE_OK",
"attributes": {
"ag": {
"metrics": {
"costs": { "cumulative": { "total": 0.0023 } },
"tokens": { "cumulative": { "total": 450 } }
}
}
}
}
}
}
}
}
}
}
Time Windows and Limits
Control which traces you retrieve using time ranges and result limits.
Time Range
Specify start and end timestamps using ISO 8601 format or Unix timestamps:
{
"oldest": "2024-01-01T00:00:00Z",
"newest": "2024-01-31T23:59:59Z"
}
The oldest timestamp is included in results. The newest timestamp is excluded.
You can also use Unix timestamps (in seconds):
{
"oldest": 1704067200,
"newest": 1706745599
}
Result Limit
Limit the number of results:
{
"limit": 100
}
Combine time ranges with limits for precise control:
{
"oldest": "2024-01-01T00:00:00Z",
"newest": "2024-01-31T23:59:59Z",
"limit": 50
}
