AI SDK 7
You are viewing the recommended integration.
AI SDK 5–6
View the legacy experimental telemetry guide.
@contextcompany/ai-sdk package registers AI SDK 7’s native OpenTelemetry integration and sends traces to The Context Company. It captures runs, model calls, tool executions, token usage, costs, sessions, users, organizations, and custom metadata.
For AI SDK 5 or 6, use the AI SDK 5–6 guide.
Set TCC environment variables
.env.local
Instrument AI SDK
If your AI SDK calls run in Next.js route handlers, use the@contextcompany/ai-sdk/nextjs entry point.
If you use another Node.js framework, use the root @contextcompany/ai-sdk entry point. It owns the OpenTelemetry NodeSDK and returns a shutdown handle.
- Next.js
- Node.js
Step 1: Install dependencies
Step 2: Add instrumentation to Next.js
Add aninstrumentation.[js|ts] file to your project root, or inside src if your application uses a src directory. See the Next.js Instrumentation guide for more information.Call registerTCC once when the Node.js runtime starts. The function is idempotent, so Next.js development reloads do not register duplicate integrations.instrumentation.ts
Step 3: Add telemetry to AI SDK calls
UsetccTelemetry on each call where you want an explicit run ID and metadata. Put TCC-specific fields and custom application fields in the same metadata object. Every TCC-specific key uses the tcc.* namespace.tcc.runId must be a UUID. Calls without an explicit run ID still receive a generated ID, but an explicit ID is recommended when you need feedback, deep links, or correlation with application data.Adding custom metadata
Custom metadata allows you to add additional properties to your agent runs. This is useful for tying agent runs to your business logic and filtering runs by environment, feature, experiment, or another application-specific dimension. Add custom fields to the samemetadata object as the reserved TCC metadata. Custom fields do not use the tcc.* namespace.
route.ts
The
tcc.* namespace is reserved for TCC-specific metadata. Use the exact supported keys shown below. Keep application-specific fields such as environment, feature, and experiment unprefixed.Adding user feedback
User feedback allows you to collect score (thumbs up and thumbs down) and text feedback of up to 2,000 characters from end users on your agent runs. This is useful for tracking user satisfaction, identifying problematic responses, and filtering agent runs in the dashboard by feedback.Step 1: Generate and pass a run ID
Generate the run ID before calling the AI SDK, then return it to your client. For streamed Next.js responses, message metadata is a convenient way to preserve it.route.ts
Step 2: Submit feedback from your client
Store the run ID on your client, then submit feedback from a server route usingsubmitFeedback.
Both score and text are optional individually, but every request must include at least one of them.
score must be either "thumbs_up" or "thumbs_down".
text can contain written feedback of up to 2,000 characters.
feedback-route.ts
Tracking agent sessions
Agent sessions group multiple agent runs. The most common use case is tracking an entire conversation between a user and an AI agent. Pass the sametcc.sessionId to every related call:
route.ts
Marking runs as conversational
A conversational run is an agent run initiated by a user. Marking a run as conversational tells The Context Company that it represents direct user interaction. Conversational runs are monitored for user insights such as confusion and frustration. Background jobs, cron tasks, and internal automations should generally remain non-conversational. Settcc.conversational to true in metadata:
route.ts
Identifying the agent
If your product ships more than one named agent, settcc.agent to scope the run to a specific agent. The dashboard’s agent selector, per-agent patterns and recaps, and the agent filter on the REST API and MCP tools read from this value.
route.ts
Agent names that collide with reserved dashboard routes (for example
runs, sessions, patterns, recaps, overview, search, failures, feedback, tools, topics, views, settings, and mcp-and-api) are dropped.Identifying users and organizations
Attach the end user and their organization to a run as first-class identity usingtcc.userId, tcc.userName, tcc.orgId, and tcc.orgName. These fields are different from similarly named custom metadata. They enable dedicated dashboard filters, user and organization search, per-user views, and per-organization analytics. See User and organization identity for the full concept.
Set these fields whenever you have a stable identifier for the user or organization:
route.ts
tcc.userName and tcc.orgName require the corresponding tcc.userId or tcc.orgId. Names without IDs are dropped.Combining multiple options
You can add all TCC tracking fields and custom metadata through onetccTelemetry call:
route.ts
tccTelemetry adds every supplied field to runtime context and includes it in AI SDK 7 telemetry. TCC-specific fields always retain their tcc.* prefix.
Multi-step agents and tools
No additional tracing code is required for tools or multi-step agents. AI SDK 7 emits the root agent operation, every model call, and every tool execution through the integration registered byregisterTCC.
route.ts
- One agent run for the full operation
- One model step for every
chatoperation - One tool-call record for every
execute_tooloperation - Token usage, cached tokens, model IDs, finish reasons, errors, and aborts
registerTCC and tccTelemetry setup. Embedding and reranking input and output values are enabled by the TCC integration.
Debug mode
Enable debug mode to log span creation, batching, and export lifecycle information while validating an integration.url to the Next.js entry point or otlpUrl to the Node.js entry point.
Examples
See the Next.js AI SDK 7 example for a complete streaming agent with tools, sessions, users, organizations, custom metadata, and feedback. If you are upgrading an existing application, follow Migrate to AI SDK 7.Local mode
Local mode allows you to run The Context Company in a local-first mode. It is 100% open-source and requires no account or API key. We also offer a free, cloud development environment for all users.
Setup
Step 1: Install dependencies
Step 2: Add instrumentation to Next.js
Add aninstrumentation.[js|ts] file to your project root, or inside src if your application uses a src directory. Register TCC with local: true.
instrumentation.ts
Step 3: Add widget to layout
Add the Local Mode widget to your root layout:app/layout.tsx
Step 4: Add telemetry to AI SDK calls
UsetccTelemetry exactly as you would in production:
Anonymous telemetry
By default, The Context Company collects limited anonymous usage data when running local mode. This helps us understand how developers use the tool and guides improvements. No sensitive or personally identifiable information is collected. You can view the exact events and values tracked in the Observatory repository. To disable anonymous telemetry, setTCC_DISABLE_ANONYMOUS_TELEMETRY to true in your Next.js project.