Exporting Traces with OpenTelemetry
This guide explains how to export KaibanJS workflow traces using the @kaibanjs/opentelemetry package. With this integration, you can visualize, debug, and monitor your AI agentsβ workflows in real time through OpenTelemetry-compatible observability tools like SigNoz, Langfuse, Phoenix, or Braintrust.
Our documentation is available in an LLM-friendly format at docs.kaibanjs.com/llms-full.txt.
Feed this URL directly into your AI IDE or coding assistant for enhanced development support!
Introductionβ
The @kaibanjs/opentelemetry package bridges KaibanJS with OpenTelemetry, automatically mapping your agent and task executions to OpenTelemetry spans.
This allows for a detailed, visual representation of how your agents think, act, and collaborate within complex workflows.
Key Featuresβ
- π Automatic Trace Mapping β KaibanJS tasks and agents are represented as OpenTelemetry spans.
- π Built-in Metrics β Duration, token usage, cost, and performance are automatically captured.
- π Multi-Service Export β Export traces to SigNoz, Langfuse, Phoenix, Braintrust, Dash0, and any OTLP-compatible service.
- βοΈ Smart Sampling β Supports configurable sampling strategies.
- π§© Zero Breaking Changes β Works without modifying your existing KaibanJS logic.
Installationβ
npm install @kaibanjs/opentelemetry
Quick Startβ
Hereβs a minimal setup to get started with OpenTelemetry tracing in your KaibanJS project:
import { Team, Agent, Task } from 'kaibanjs';
import { enableOpenTelemetry } from '@kaibanjs/opentelemetry';
const team = new Team({
name: 'My Observability Team',
agents: [...],
tasks: [...]
});
const config = {
enabled: true,
sampling: { rate: 1.0, strategy: 'always' },
attributes: {
includeSensitiveData: false,
customAttributes: {
'service.name': 'kaiban-observability-demo',
'service.version': '1.0.0'
}
},
exporters: {
console: true,
otlp: {
endpoint: 'https://ingest.us.signoz.cloud:443',
protocol: 'grpc',
headers: { 'signoz-access-token': 'your-token' },
serviceName: 'kaibanjs-service'
}
}
};
enableOpenTelemetry(team, config);
await team.start({ input: 'data' });
Configuration Optionsβ
OpenTelemetryConfig Interfaceβ
interface OpenTelemetryConfig {
enabled: boolean;
sampling: {
rate: number;
strategy: 'always' | 'probabilistic' | 'rate_limiting';
};
attributes: {
includeSensitiveData: boolean;
customAttributes: Record<string, string>;
};
exporters?: {
console?: boolean;
otlp?: OTLPConfig | OTLPConfig[];
};
}
Sampling Strategiesβ
| Strategy | Description |
|---|---|
always | Records all traces β recommended for development |
probabilistic | Samples a percentage of traces (0.0 to 1.0) |
rate_limiting | Limits trace rate for high-load production systems |
Trace Structureβ
The package creates simplified traces with the following structure:
Task Span (CLIENT) - DOING β DONE
βββ Agent Thinking Span (CLIENT) - THINKING β THINKING_END
βββ Agent Thinking Span (CLIENT) - THINKING β THINKING_END
βββ Agent Thinking Span (CLIENT) - THINKING β THINKING_END
Span Hierarchyβ
- Task Spans: Individual task execution spans
- Agent Thinking Spans: Nested spans for agent LLM interactions
Span Kindsβ
The package automatically determines span kinds based on span names:
- CLIENT (2): Task and Agent spans - represent client operations
- INTERNAL (0): Default for other spans - internal operations
Event Mappingβ
The package automatically maps KaibanJS workflow events to OpenTelemetry spans:
Task Eventsβ
| KaibanJS Event | OpenTelemetry Span | Description |
|---|---|---|
TaskStatusUpdate | Task Span | Task execution lifecycle events |
DOING | Task Span Start | Task execution started |
DONE | Task Span End | Task completed successfully |
AWAITING_VALIDATION | Task Span | Task awaiting validation |
VALIDATED | Task Span | Task validated successfully |
ERRORED | Task Span Error | Task failed with error |
ABORTED | Task Span Abort | Task aborted |
Agent Eventsβ
| KaibanJS Event | OpenTelemetry Span | Description |
|---|---|---|
AgentStatusUpdate | Agent Thinking Span | Agent thinking and execution events |
THINKING | Agent Thinking Span Start | Agent begins thinking process |
THINKING_END | Agent Thinking Span End | Agent completes thinking process |
KaibanJS Semantic Conventionsβ
The package uses KaibanJS-specific semantic conventions for LLM attributes that are automatically recognized by observability services:
LLM Request Attributes (kaiban.llm.request.*)β
kaiban.llm.request.messages- Input messages to the LLMkaiban.llm.request.model- Model name used for the requestkaiban.llm.request.provider- Provider of the model (openai, anthropic, google, etc.)kaiban.llm.request.iteration- Iteration number for the thinking processkaiban.llm.request.start_time- When the thinking process startedkaiban.llm.request.status- Status of the request (started, interrupted, completed)kaiban.llm.request.input_length- Length of the input messageskaiban.llm.request.has_metadata- Whether metadata is availablekaiban.llm.request.metadata_keys- Available metadata keys
LLM Usage Attributes (kaiban.llm.usage.*)β
kaiban.llm.usage.input_tokens- Number of input tokenskaiban.llm.usage.output_tokens- Number of output tokenskaiban.llm.usage.total_tokens- Total tokens usedkaiban.llm.usage.prompt_tokens- Prompt tokenskaiban.llm.usage.completion_tokens- Completion tokenskaiban.llm.usage.cost- Cost in USD
LLM Response Attributes (kaiban.llm.response.*)β
kaiban.llm.response.messages- Output messages from the LLMkaiban.llm.response.duration- Duration of the responsekaiban.llm.response.end_time- When the response endedkaiban.llm.response.status- Status of the response (completed, error, etc.)kaiban.llm.response.output_length- Length of the output messages
Task Attributes (task.*)β
task.id- Unique task identifiertask.name- Task titletask.description- Task descriptiontask.status- Task status (started, completed, errored, aborted)task.start_time- When task execution startedtask.end_time- When task execution endedtask.duration_ms- Task execution duration in millisecondstask.iterations- Number of iterations performedtask.total_cost- Total cost for the tasktask.total_tokens_input- Total input tokens usedtask.total_tokens_output- Total output tokens generatedtask.has_metadata- Whether task has metadatatask.metadata_keys- Available metadata keys
Agent Attributes (agent.*)β
agent.id- Unique agent identifieragent.name- Agent nameagent.role- Agent role description
Error Attributes (error.*)β
error.message- Error messageerror.type- Error typeerror.stack- Error stack trace
Span Typesβ
task.execute- Task execution spanskaiban.agent.thinking- Agent thinking spans (nested under task spans)
These conventions ensure that observability services like Langfuse, Phoenix, and others can automatically recognize and properly display LLM-related data in their dashboards.
Span Context Managementβ
The package uses a KaibanSpanContext to manage span relationships and correlation across workflows:
Context Structureβ
interface KaibanSpanContext {
teamName: string;
workflowId: string;
rootSpan?: Span;
taskSpans: Map<string, Span>;
agentSpans: Map<string, Span>;
}
Context Methodsβ
-
Root Span Management:
setRootSpan(span: Span)- Set the workflow root spangetRootSpan()- Get the current root span
-
Task Span Management:
setTaskSpan(taskId: string, span: Span)- Associate a span with a taskgetTaskSpan(taskId: string)- Retrieve task spanremoveTaskSpan(taskId: string)- Remove task span from context
-
Agent Span Management:
setAgentSpan(agentId: string, span: Span)- Associate a span with an agentgetAgentSpan(agentId: string)- Retrieve agent spanremoveAgentSpan(agentId: string)- Remove agent span from context
Context Lifecycleβ
- Task Execution: Task spans are created
- Agent Thinking: Agent thinking spans are nested under task spans
- Task Completion: All spans are completed and context is cleared
Span Correlationβ
The context ensures proper parent-child relationships between spans:
- Task spans are parents of agent thinking spans
- All spans maintain proper trace context for distributed tracing
Exporting Tracesβ
Console Exporter (for Development)β
exporters: {
console: true;
}
OTLP Exporter (for Production)β
You can export traces to any OTLP-compatible service.
Example: Single Serviceβ
exporters: {
otlp: {
endpoint: 'https://cloud.langfuse.com/api/public/otel',
protocol: 'http',
headers: {
Authorization: 'Basic ' + Buffer.from('pk-lf-xxx:sk-lf-xxx').toString('base64')
},
serviceName: 'kaibanjs-langfuse'
}
}
Example: Multiple Servicesβ
exporters: {
otlp: [
{
endpoint: 'https://ingest.us.signoz.cloud:443',
protocol: 'grpc',
headers: { 'signoz-access-token': 'your-token' },
serviceName: 'kaibanjs-signoz'
},
{
endpoint: 'https://cloud.langfuse.com/api/public/otel',
protocol: 'http',
headers: {
Authorization:
'Basic ' + Buffer.from('pk-lf-xxx:sk-lf-xxx').toString('base64')
},
serviceName: 'kaibanjs-langfuse'
}
];
}
Environment Variable Configurationβ
export OTEL_EXPORTER_OTLP_ENDPOINT="https://your-service.com"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer your-token"
export OTEL_EXPORTER_OTLP_PROTOCOL="http"
Then in your code:
exporters: {
otlp: {
serviceName: 'kaibanjs-service';
}
}
Monitoring Metricsβ
- Workflow and task duration
- Cost and token usage
- Iteration count
- Error rates
- Resource consumption
Advanced Usageβ
import { createOpenTelemetryIntegration } from '@kaibanjs/opentelemetry';
const integration = createOpenTelemetryIntegration(config);
integration.integrateWithTeam(team);
await team.start({ input: 'data' });
await integration.shutdown();
Best Practicesβ
- Use probabilistic sampling in production.
- Avoid including sensitive data in traces.
- Validate exporter endpoints and authentication tokens.
- Use the console exporter for local debugging.
- Monitor memory and performance when scaling agents.
Troubleshootingβ
| Issue | Possible Cause | Solution |
|---|---|---|
| Connection refused | Wrong endpoint | Verify OTLP URL and protocol |
| Authentication failed | Invalid API token | Double-check headers or environment variables |
| Timeout errors | Network latency | Increase timeout in OTLP config |
| No traces visible | Sampling rate too low | Use strategy: 'always' temporarily |
Conclusionβ
By integrating OpenTelemetry with KaibanJS, you gain deep visibility into your agentsβ behavior and task performance.
This observability layer empowers you to diagnose issues faster, optimize execution flows, and scale AI systems confidently.
Found this guide useful or have suggestions?
Help us improve by submitting an issue on GitHub.