Skip to main content

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.

Your account

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 200 and 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 is AGENT_NAME in src/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/chat both use.
  • Conversation ID default for the chat page (one long conversation), and a new api-… for every /api/chat call (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:

  1. In wrangler.jsonc, replace the "observability" line with "observability": { "enabled": true, "traces": { "enabled": true } }.
  2. In src/server.ts, add the two imports at the top, and delete generateText and streamText from the import { … } from "ai" line (keep the others).
  3. Below the MODEL line, add the wrapAISDK statement, AGENT_NAME and traceIdentity() from the block above.
  4. Add ...traceIdentity(this.name, this.name), as the last option of the streamText({ … }) call in onChatMessage, and ...traceIdentity(this.name, `api-${crypto.randomUUID()}`), as the last option of the generateText({ … }) call in chat().
  5. npm run check, then npm 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:

Type it in your agent's chat page
What is the weather in Stockholm?

Open your agent in a browser and paste this into its message box.

Type it in your agent's chat page
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:

macOS / Linux
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:

  1. Where did the time go? Almost all of it is the chat spans: the model. Tools such as calculate take about 0 ms.
  2. Which tool did the model pick, with which arguments? Open the chat span: its output is the tool call calculate with 2500, 4 and *. The tool has not run yet: here execute_tool calculate only holds the approval request.
  3. Where did a person decide? tool_approval calculate with the state requested: 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 cf CLI, logged in to your account.

Copy this prompt into your coding agent, with your account id from your card filled in. It only reads.

Prompt for your coding agent

What your agent will do

  1. Reads the last 30 minutes of your agent's traces, through the Cloudflare MCP server or the cf CLI, and changes nothing.
  2. Reports runs, tokens, the slowest run and its slowest part, the tools and approvals, and any errors.
  3. 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.

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:

  1. Sample. Every request is traced now (head_sampling_rate defaults to 1). With real traffic, add "head_sampling_rate": 0.05 (5%) under "traces". Sampling is decided per request. Workers traces
  2. 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
  3. Decide what you record. storeMessages and storeTools keep 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 into runtimeContext.
  4. Name things well. One fixed AGENT_NAME per 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.
  5. Trace every model call. A call in a Workflow or another file needs the same wrap and identity, or it does not show.
  6. 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).
  7. Send traces where your team looks. OpenTelemetry export to Honeycomb, Grafana, Axiom and others. OpenTelemetry export
  8. Get told instead of looking. Custom alerts (beta) on Worker events and traces, for example errors after a deploy. Alerts
  9. And one line each:
    • AI Gateway: each chat span 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

Check it worked​

  • chat-agent is in the Agents view, with at least one session.
  • One trace shows chat and execute_tool spans.
  • The 2500 * 4 turn shows tool_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.jsonc needs "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 generateText and streamText are not in your import { … } 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 streamText from "ai", and the starter's traced one is already declared: delete that name from the pasted import.
  • Many agents with odd names. functionId must 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_approval and execute_tool spans 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 in npx 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 cf CLI logged in to that account. Otherwise use the dashboard.
Go further

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).