Spans and Traces
Copy page
OpenTelemetry spans for distributed tracing and observability in the Inkeep Agent Framework
Overview
The Inkeep Agent Framework uses OpenTelemetry for distributed tracing and observability. Spans provide detailed visibility into the execution flow of agents, context resolution, tool execution, and other framework operations.
Getting Started with Spans
1. Import Required Dependencies
2. Get the Tracer
Creating and Using Spans
Setting Span Attributes
Basic Attributes
Adding Events to Spans
Recording Important Milestones
Error Events
Error Handling and Status
Using setSpanWithError Utility
The framework provides a convenient setSpanWithError utility function that handles error recording and status setting:
Cache Telemetry Attributes
LLM generation spans carry cache telemetry emitted by the gateway cost middleware. These attributes are defined in packages/agents-core/src/constants/otel-attributes.ts and consumed by the Manage UI trace timeline and cost dashboard.
Outcome attributes (OTel-GenAI semconv v1.41.1 aligned)
| Attribute | Type | Description |
|---|---|---|
gen_ai.usage.cache_read.input_tokens | number | Tokens served from the provider cache on this call |
gen_ai.usage.cache_creation.input_tokens | number | Tokens written to the provider cache on this call |
gen_ai.usage.input_tokens (the existing total) is cache-inclusive and unchanged. no_cache_input_tokens is derivable as input_tokens − cache_read − cache_creation and is not emitted.
Intent attributes (Inkeep cache.intent.* namespace)
| Attribute | Type | Description |
|---|---|---|
cache.intent.marker_count | int (0–4) | Number of cache markers attached to the request prefix |
cache.intent.prefix_signature | string | 10-character SHA-256 hex prefix of the cacheable prefix (system prompts and tool definitions), used to distinguish MISS-expected (prefix changed) from MISS-regression (stable prefix, no read) |
Cache state (consumer-derived, not a span attribute)
cache_state is not emitted as a span attribute — it is computed by consumers (Manage UI, the pnpm cache-debug CLI) from the four attributes above plus prior-turn signature comparison:
| State | Derivation |
|---|---|
HIT | marker_count > 0 and cache_read > 0 |
MISS-expected | marker_count > 0, cache_read = 0, prefix signature differs from previous call |
MISS-regression | marker_count > 0, cache_read = 0, prefix signature identical to previous call |
NOT-ATTEMPTED | marker_count = 0 |
NOT-SUPPORTED-BY-PROVIDER | Provider metadata absent or provider does not support marker-based caching |
When adding new span attributes, follow the same append-only rule: additions to USAGE_COST_AGGREGATION_ORDER in signoz-stats.ts must be appended, never reordered, because the consumer side uses positional indexing.
Best Practices
1. Consistent Naming Convention
The span naming convention follows a hierarchical structure that mirrors your code organization.
Naming Rules
- Class First: Start with the class/module name (e.g.,
agent,context,tool) - Function Second: Follow with the specific function/method (e.g.,
generate,resolve) - Use Underscores: For multi-word functions, use underscores (e.g.,
tool_execute,cache_lookup) - Consistent Casing: Use lowercase with underscores for consistency
Configuration and Setup
Environment Variables
Instrumentation Setup
The framework automatically sets up OpenTelemetry instrumentation in src/instrumentation.ts:
Examples in the Codebase
Agent Operations
See src/agents/Agent.ts for span usage in agent generation and tool execution.
Example from Agent.ts:
Summary
Spans provide powerful observability into your Inkeep Agent Framework operations. By following these patterns:
- Use
getTracer()for consistent tracing - Use consistent naming
- Set meaningful attributes for searchability
- Handle errors properly using
setSpanWithErrorfor consistent error handling - Use
startActiveSpanfor automatic lifecycle management
This will give you comprehensive visibility into your agent operations, making debugging and performance optimization much easier.