All examples are available on GitHub: keywordsai-example-projects/typescript/tracing_sdk_example
Setup
All examples require:.env file:
.env
Basic Usage
Frombasic_usage.ts. Core concepts: withWorkflow, withTask, withAgent, withTool.
OpenAI Integration
Fromopenai_integration.ts. Automatic instrumentation of the OpenAI SDK.
Advanced Agentic Workflow
Fromadvanced_tracing.ts. Demonstrates withAgent, withTool, and custom metadata for complex agent workflows.
Span Management
UsinggetClient() API to manually update spans, add events, and record exceptions.
Multi-Provider Tracing
Frommulti_provider.ts. Trace across multiple AI providers (OpenAI + Anthropic) in a single workflow.
Agent with Multiple Tools
Example of an assistant agent using multiple tools for analysis and response generation.API Reference
| Method | Description |
|---|---|
withWorkflow | High-level grouping of tasks (top-level trace) |
withTask | Discrete step within a workflow |
withAgent | Specialized for agentic patterns |
withTool | Tool/function calls within agents |
getClient() | Access manual span management methods |
initialize() | Initialize tracing (required) |
shutdown() | Gracefully shutdown and flush spans |
Client Methods
| Method | Description |
|---|---|
updateCurrentSpan | Update name, attributes, status, KeywordsAI params |
addSpanEvent | Add a timestamped event to current span |
recordSpanException | Record an error on current span |
getCurrentTraceId | Get the current trace ID |
getCurrentSpanId | Get the current span ID |
flush | Manually flush pending spans |
More Examples
Explore additional examples in the GitHub repository:- Instrumentation Management (
instrumentation_management.ts): Disabling specific instrumentations, explicit module loading - Manual Instrumentation (
manual_instrumentation.ts): For Next.js and environments where dynamic imports donβt work - Span Buffering (
span_buffering.ts): Manual control over span creation and batch processing - Noise Filtering (
noise_filtering.ts): How the SDK filters out auto-instrumentation noise - Multi-Processor (
multi_processor.ts): Custom span processors with routing and filters