Observe it (bonus)
By the end you will be able to:
- Find your agent in the Agents view and replay a conversation
- Read one trace: where the time went, which tool ran, where a person approved
- Let your coding agent read your traces, and know what to change before production
Optional and not scored. It works as soon as your agent is deployed (Step 1: Launch your first agent), and it is most useful after Step 5: Human approval, when there is an approval to look at. Take ten minutes for it whenever you like.
Checking your assignment… If it cannot load, ask a host and continue with the guide.
The account id and hostname below are placeholders. They appear here once a host assigns you to a team and an account.
- Account
shown here once your account is ready- Account id
YOUR_ACCOUNT_ID- Demo hostname
YOUR_HOSTNAME
Use the Cloudflare account on your card (also on the Workspace page).
Why it matters
- An agent can answer
200and still get it wrong. It called the wrong tool, retried in a loop, or ran an action nobody approved. The reply alone does not tell you. - Before anyone lets an agent near production, they ask three things: what did it do, why, and what did it cost. A trace answers all three from a record, not from memory.
- It is already on. The starter sends every turn of your agent to your own dashboard. You only have to look.
The concepts
- Turn: one message to your agent and its answer.
- Session: a conversation made of one or more turns.
- Trace: the record of one turn, as a waterfall of timed steps.
- Span: one timed step inside a trace: a model call, a tool run, an approval.
What one turn looks like. This is the span tree our test agent produced for the weather question, and for 2500 * 4, which asks for approval:
invoke_agent chat-agent one turn
├── chat @cf/google/gemma-4-26b-a4b-it the model decides to call a tool
├── execute_tool getWeather the tool runs
└── chat @cf/google/gemma-4-26b-a4b-it the model answers with the result
invoke_agent chat-agent
├── chat @cf/google/gemma-4-26b-a4b-it
└── execute_tool calculate
└── tool_approval calculate state: requested
Who is who. Each trace carries three names, filled in by the starter:
- Agent name
chat-agent: your agent in the list. It isAGENT_NAMEinsrc/server.ts: rename it to what your agent does (one fixed name, never a user or a conversation id). - Agent ID
chat-agent/default: the agent instance the chat page and/api/chatboth use. - Conversation ID
defaultfor the chat page (one long conversation), and a newapi-…for every/api/chatcall (each one is a one-off question).
How the starter does it. Three pieces, already in your download:
// wrangler.jsonc: logs and traces
"observability": { "enabled": true, "traces": { "enabled": true } }
// src/server.ts: the traced generateText and streamText
import { wrapAISDK } from "agents/observability/ai";
import * as ai from "ai";
const { generateText, streamText } = wrapAISDK(ai, {
storeMessages: true,
storeTools: true
});
// the name and the identity of each model call
const AGENT_NAME = "chat-agent";
/** Trace identity for one model call: which agent instance, which conversation. */
function traceIdentity(instance: string, conversationId: string) {
return {
runtimeContext: { agentId: `${AGENT_NAME}/${instance}`, conversationId },
telemetry: {
functionId: AGENT_NAME,
includeRuntimeContext: { agentId: true, conversationId: true }
}
};
}
// on each model call: onChatMessage (the chat page) ...
...traceIdentity(this.name, this.name)
// ... and chat() (POST /api/chat, a new id per call)
...traceIdentity(this.name, `api-${crypto.randomUUID()}`)
What is recorded, where, for how long. The starter records the messages, the model's answers, and every tool's arguments and results (storeMessages and storeTools). They stay 7 days in your own account, visible to anyone with access to it. Your AI Gateway log already keeps each prompt and answer too. Use synthetic data for this workshop. Your demo URL and chat may be publicly reachable; do not upload real customer, employee or proprietary data.
Three ways to see what happened, and when to use each:
- AI Gateway log: one model call: the prompt, the answer, the cost. It does not show your tools.
- Trace (this page): the whole turn: every model call, tool and approval, how long each took, the tokens.
npx wrangler tail: live logs with the full error text. A trace keeps only the error's type.
Starter older than 2026.11.2? Add the three pieces
Check the version you downloaded in Step 1: 2026.11.1 and older have logs but no traces. Either download 2026.11.2 into a fresh folder and copy your src/server.ts changes over, or add the three pieces by hand:
- In
wrangler.jsonc, replace the"observability"line with"observability": { "enabled": true, "traces": { "enabled": true } }. - In
src/server.ts, add the two imports at the top, and deletegenerateTextandstreamTextfrom theimport { … } from "ai"line (keep the others). - Below the
MODELline, add thewrapAISDKstatement,AGENT_NAMEandtraceIdentity()from the block above. - Add
...traceIdentity(this.name, this.name),as the last option of thestreamText({ … })call inonChatMessage, and...traceIdentity(this.name, `api-${crypto.randomUUID()}`),as the last option of thegenerateText({ … })call inchat(). npm run check, thennpm run deploy.
A model call in another file (a Workflow step, src/mcp.ts) is traced only if it uses the same wrap: import wrapAISDK and ai there too, and pass the same identity. Otherwise that call does not show in the Agents view.
Do it
1. Give your agent something to do
Traces come from your deployed agent (npm run deploy), not from npm run dev. Send it three turns:
What is the weather in Stockholm?
Open your agent in a browser and paste this into its message box.
Calculate 2500 * 4
Open your agent in a browser and paste this into its message box.
Click Approve on the card. Then one call over /api/chat:
curl -s -X POST "$DEMO_URL/api/chat" \
-H 'content-type: application/json' \
-d '{"message":"What is the weather in Gothenburg?"}'
You should see: normal answers. Give the traces about 30 seconds to arrive.
2. Find your agent in the Agents view
Open the Agents view in the dashboard (signed in to the account on your card; in the menu it is AI > Agent tracing). You should see: chat-agent in the list, with its model, its sessions, its runs and the tokens used. Select it: it lists its traces, newest first, each with its duration, tokens and status.
3. Replay the conversation
Open a trace of the chat page and choose Session replay. You should see: the whole chat-page conversation (default) across turns: your messages, the answers, and each tool call with its arguments and result. The /api/chat call is its own session.
4. Read one trace
Open the trace of the 2500 * 4 turn and choose Trace (the waterfall). Ask it three questions:
- Where did the time go? Almost all of it is the
chatspans: the model. Tools such ascalculatetake about 0 ms. - Which tool did the model pick, with which arguments? Open the
chatspan: its output is the tool callcalculatewith2500,4and*. The tool has not run yet: hereexecute_tool calculateonly holds the approval request. - Where did a person decide?
tool_approval calculatewith the staterequested: the turn stopped there and waited for your click.
Then look at the tokens: input and output, per chat span and for the whole turn. You should see: the input tokens are most of it (the system prompt and the tools go with every call).
After your click. Approve (or Reject) continues the work in a new, short trace: its tool_approval calculate says approved (or denied), and execute_tool calculate holds the arguments and the result. In our test that trace had no agent name, and its last model call was cut short (the chat library finishes that part after the request has ended). So it is not listed under chat-agent or in its sessions: find it in View in Observability, right after the turn that asked. The same happens after a browser tool (getUserTimezone) answers, and a turn you Stop is not listed either.
An approval span is short even if you waited a minute before clicking: by design, it does not measure the time a person takes.
The dashboard may word some labels differently from this page; the docs have current screenshots.
Let your coding agent investigate
Your coding agent can read the same traces and tell you what they mean. It needs one of these:
- The Cloudflare MCP server from Step 0 (
cloudflare,https://mcp.cloudflare.com/mcp), authorised for your own account with the Workers Observability permission. The query needs the write permission even to read; the consent screen lets you pick it. - The
cfCLI, logged in to your account.
Copy this prompt into your coding agent, with your account id from your card filled in. It only reads.
What your agent will do
- Reads the last 30 minutes of your agent's traces, through the Cloudflare MCP server or the
cfCLI, and changes nothing. - Reports runs, tokens, the slowest run and its slowest part, the tools and approvals, and any errors.
- Suggests one change, and how you would see its effect in the next trace.
Key ideas to understand
- Trace and span: a trace is one turn; each span inside it is one timed step: a model call, a tool run, an approval.
- Tokens: input tokens are most of the cost, because the system prompt and the tool list go with every model call.
- Errors in a trace: a trace keeps only the error type. The message is in
npx wrangler tail.
More in The concepts.
I am doing the Observe it bonus of Build with Cloudflare Stockholm. Only read: do not change any code, setting or resource.
My Cloudflare account is <ACCOUNT ID FROM MY CARD> and my Worker is called agent. Use only that account.
Query my Workers Observability telemetry for the last 30 minutes with the "agents" view: through the Cloudflare MCP server (cloudflare, https://mcp.cloudflare.com/mcp), or with `cf o11y telemetry query` if I have the cf CLI. An ad-hoc query needs any queryId plus "parameters": {}. If neither works, tell me why and stop.
Turns that continue after an Approve or Reject click can show without an agent name: count them too, and read their spans for the approval. The same span can come back from two datasets: count it once.
Then tell me, in plain language and in under 250 words:
1. How many runs and conversations my agent chat-agent had, and how many tokens they used.
2. The slowest run: how long it took, and which part (a model call or a tool) took most of that time.
3. Which tools ran and how often, and every approval that was requested, approved or rejected. A tool call below its approval threshold that ran without asking is expected: check the arguments before calling it a problem.
4. Any errors. A trace keeps only the error type, so do not guess the cause: tell me to run npx wrangler tail and repeat the request to see the message.
Finish with one change that would make my agent faster, cheaper or safer, and how I would see its effect in the next trace.Paste it into your coding agent: the terminal, the IDE panel or the desktop app all work.
A good answer names your real number of runs, the slowest run and its longest span (usually a chat call), the tools that ran, each approval with its state, and one concrete change. If your coding agent cannot reach your traces, use the dashboard: everything above is there too.
Show it in your demo (optional)
In the "Cloudflare pieces" beat, spend ten seconds on the trace of the turn that asked for approval: point at the tool_approval line and say "this is the record of what the agent did, and where a person had to say yes". Open the tab before you start. Your two minutes do not change.
Production checklist
What to change before real users talk to your agent:
- Sample. Every request is traced now (
head_sampling_ratedefaults to1). With real traffic, add"head_sampling_rate": 0.05(5%) under"traces". Sampling is decided per request. Workers traces - Know the bill. From 1 December 2026, logs, traces and Issues share one allowance per account. Free: 0.5 GB ingested per day, 7 days kept, ingestion stops at the limit until 00:00 UTC. Paid: 50 GB ingested and 10 GB-month stored per billing cycle, then $0.25 per GB and $0.10 per GB-month. Observability pricing
- Decide what you record.
storeMessagesandstoreToolskeep what users type and what tools return. Where that may be personal data, turn them off (wrapAISDK(ai)with no options), or keep them on in a test environment only. Never put a secret intoruntimeContext. - Name things well. One fixed
AGENT_NAMEper agent. The agent ID is your agent instance, the conversation ID your real session id. Never a user id or an email in the name. - Trace every model call. A call in a Workflow or another file needs the same wrap and identity, or it does not show.
- Keep your own record of decisions. Traces are for debugging: they are not a complete record, and they are kept 7 days. For an audit trail, store each approval in your own table (Step 5: Human approval, Go further).
- Send traces where your team looks. OpenTelemetry export to Honeycomb, Grafana, Axiom and others. OpenTelemetry export
- Get told instead of looking. Custom alerts (beta) on Worker events and traces, for example errors after a deploy. Alerts
- And one line each:
- AI Gateway: each
chatspan carries the gateway log id (cloudflare.ai_gateway.log.id), to look up that call's cost, cache and guardrail result in AI Gateway Logs. Cap spend with spend limits. - Workers Issues groups your Worker's errors into issues. Issues
- Custom spans time your own steps inside a turn. Custom spans
- Cloudflare Traces (open beta) follows a request through the WAF, cache and routing before it reaches your Worker. Cloudflare Traces
- AI Gateway: each
Check it worked
chat-agentis in the Agents view, with at least one session.- One trace shows
chatandexecute_toolspans. - The
2500 * 4turn showstool_approval calculate. - Session replay shows your conversation.
- Optional: your coding agent's answer names your real number of runs.
Stuck?
- Your agent is not in the Agents view. Wait about 30 seconds and reload. Only deployed traffic shows, not
npm run dev.wrangler.jsoncneeds"traces": { "enabled": true }, deployed since. The dashboard must be on the account from your card. - Observability shows Worker traces but no agent. A model call does not go through the traced functions. Check that
generateTextandstreamTextare not in yourimport { … } from "ai"line, and that each call passes...traceIdentity(...). - "Duplicate identifier 'streamText'" (or "Import declaration conflicts…") after pasting a snippet, such as the Platform Guide's. The snippet imports
streamTextfrom "ai", and the starter's traced one is already declared: delete that name from the pasted import. - Many agents with odd names.
functionIdmust be one fixed name (AGENT_NAME), not a user or a conversation id. - Your Approve or Reject (or a stopped turn) is missing from your agent's runs. Expected with the starter (see "After your click" above): the work after the click finishes outside the request, so that trace has no agent name. Find it in View in Observability; its
tool_approvalandexecute_toolspans are there. - Session replay is empty or cut short. Payload recording is off, a message was too long (payloads can be truncated), or it was an image (not shown).
- The approval span is short although you waited. By design: approval spans do not measure the time a person takes.
- A failed run shows no error message. A trace keeps only the error type (for a missing gateway,
AI_APICallError). The message is innpx wrangler tail. - Your coding agent cannot read traces. It needs the Cloudflare MCP server from Step 0, authorised for your own account with the Workers Observability permission (the query needs it even to read), or the
cfCLI logged in to that account. Otherwise use the dashboard.
Go further
- Agent tracing: the span structure, identity and payload settings.
- Workers traces and custom spans.
- OpenTelemetry export.
- Observability pricing.
- Cloudflare's MCP servers.
Back: Step 6: Secure it. Next: Step 7: Demo and submit.
Need help? Raise a hand for a host, or ask the Mentor dock (it escalates to a host when unsure).