Overview
Cloudflare Think uses the Vercel AI SDK internally. Think owns thestreamText
call, so add Latitude telemetry in beforeTurn() instead of at a model call
site.
Start with the basic setup. Add context only if you need traces to include
user, session, tags, or metadata from your app.
Requirements
- A Latitude API key
- A Latitude project slug
- A Cloudflare Workers project using
@cloudflare/think - The
nodejs_compatcompatibility flag enabled in your Worker LATITUDE_API_KEYandLATITUDE_PROJECT_SLUGconfigured as Worker secrets or variables
Basic Telemetry
This records Think model calls, tools, latency, tokens, prompts, and responses.1
Install
2
Initialize Latitude once
process.env, this is all you need.
Most Workers receive secrets through env bindings instead. In that case,
keep one Latitude instance per Worker isolate:getLatitude(this.env).getTracer("cloudflare-think") in
beforeTurn() and getLatitude(this.env).flush() in onChatResponse() /
onChatError(). Do not create new Latitude() inside beforeTurn().Add App Context
If your app uses Think’s default WebSocket chat entrypoint, pass the same context you would normally pass tocapture() through useAgentChat({ body }).
beforeTurn() and pass it to getTracer():
Codemode Internal Tools
Requires@latitude-data/telemetry 3.6.0 or newer.
Think’s codemode execute tool appears as a normal AI SDK tool span. Tools
called from inside codemode run outside the active AI SDK tool-call context, so
wrap both the internal tool set and the outer execute tool with
createCodemodeTelemetry().
The helper records each internal codemode tool as an ai.toolCall <name> child
span under execute, stamps the same Latitude context as the turn, records AI
SDK and GenAI tool attributes, and marks failed tools with exception details.
When concurrent execute calls cannot be correlated after crossing the codemode
sandbox boundary, ambiguous internal spans are omitted instead of attaching them
to the wrong trace.
Programmatic Turns
If your app starts Think turns with your ownrunTurn() call, wrap that call
with capture().
getTracer() in beforeTurn():
If You Cannot Wrap The Turn
If you cannot wrap the turn, pass context directly to the tracer frombeforeTurn(). The context can come from agent state, Durable Object storage,
auth state, or any place your agent can read during the turn.
cloudflare-think-turn span.
Runnable Example
The Latitude repository includes a runnable Think example atexamples/cloudflare-think-app.
It includes an execute codemode tool backed by several demo tools, a small QA
page, and a local verifier that checks model spans, codemode tool spans,
userId, and sessionId against local Latitude.
Seeing Your Traces
Once connected, traces appear automatically in Latitude:- Open your project in the Latitude dashboard
- Send a message to your Think agent
- The turn appears with model calls, tool calls, messages, latency, token usage, and errors