Skip to content

6. Agent tracing

Checkpoint 6 of 67 minutes

Agent tracing

Replay one FieldTrip turn and connect its agent spans with a structured forecast log.

Flue 2.0.0 and newer adds agent-shaped spans to Cloudflare automatically. One response becomes an invoke_agent trace, each model turn becomes a chat span, and each tool call becomes an execute_tool span. The trace keeps the causal tree and timing. Cloudflare’s consolidated observability experience puts those traces beside the Worker’s structured application logs.

This checkpoint follows cp/6-iterate-observe: it enables Workers Logs and Traces in wrangler.jsonc and adds one structured forecast log in src/tools/weather.ts. Flue installs its tracing adapter at build time, so no manual observe() callback is needed.

Update wrangler.jsonc to enable Workers Logs and Traces. No observability SDK is required:

wrangler.jsonc+4−1

Changes from cp/5-sandbox → cp/6-iterate-observe

wrangler.jsonc
===================================================================
--- a/wrangler.jsonc cp/5-sandbox
+++ b/wrangler.jsonc cp/6-iterate-observe
@@ -26,5 +26,8 @@
// cp5: container sandbox (class exported from src/cloudflare.ts, image from ./Dockerfile).
"durable_objects": { "bindings": [{ "name": "Sandbox", "class_name": "Sandbox" }] },
- "containers": [{ "class_name": "Sandbox", "image": "./Dockerfile", "max_instances": 10 }]
+ "containers": [{ "class_name": "Sandbox", "image": "./Dockerfile", "max_instances": 10 }],
+
+ // cp6: Workers Logs + Traces (Cloudflare Agents and consolidated Observability).
+ "observability": { "enabled": true, "traces": { "enabled": true } }
}

In src/tools/weather.ts, add the structured log after parsing daily and immediately before the forecast return. The diff shows the insertion in context; Complete file includes both weather tools:

src/tools/weather.ts+1−0

Changes from cp/5-sandbox → cp/6-iterate-observe

src/tools/weather.ts
===================================================================
--- a/src/tools/weather.ts cp/5-sandbox
+++ b/src/tools/weather.ts cp/6-iterate-observe
@@ -81,6 +81,7 @@
weather_code: number[];
};
};
+ console.log({ event: 'forecast', latitude: data.latitude, longitude: data.longitude, days: daily.time.length }); // cp6: Workers Logs
return {
output: daily.time.map((date, i) => ({
date,

The trace already records the tool boundary and outbound fetch. This log adds the application fact that a forecast was successfully parsed and how many days it contained. It is diagnostic data, not model context.

Trip dates are filled automatically in the commands and their copy buttons. The guide reuses your workshop dates.

Terminal window
npm run typecheck
npm run deploy
sleep 5
npm run smoke -- https://field-trip-agent.<subdomain>.workers.dev cp6-live "Offsite in Lisbon from <START> to <END> for 14 people. Save the brief, then check the weather."

Use a fresh conversation ID and the exact deployed URL. The response should save the brief, call geocode_city, call get_forecast, and return the forecast.

Save your workers.dev subdomain to open this test ↗https://field-trip-agent.<subdomain>.workers.dev/?id=cp6-live

This opens the same conversation as the smoke test. If you repeat this checkpoint, before copying its commands.

After the live response completes, allow a couple of minutes for ingestion and refresh if the latest run is not visible yet:

  1. Open Agents in the Cloudflare dashboard.
  2. Select FieldTrip, then open the newest session whose conversation ID starts with cp6-live-.
  3. Use Session replay to follow the user message, model output, and tool calls in order.
  4. Confirm that the replay shows save_trip_brief, geocode_city, and get_forecast.

Session replay answers what happened. The trace waterfall answers where time was spent and which operation called which.

Open Trace for the same turn:

  1. Expand invoke_agent FieldTrip, the span for the complete response.
  2. Inspect each chat @cf/google/gemma-4-26b-a4b-it span for latency and token usage.
  3. Inspect execute_tool save_trip_brief, execute_tool geocode_city, and execute_tool get_forecast.
  4. Expand get_forecast and follow its outbound fetch to Open-Meteo.
  5. Compare the span durations. Do not add parent and child durations because they overlap.

The admission POST returns before the durable response finishes. The agent run in the Agents dashboard is the complete turn, not only that short HTTP request.

Open the account-level Observability dashboard:

  1. Filter to the field-trip-agent Worker and the time range of your cp6-live-… run.
  2. In Events, filter event = forecast and open the successful forecast log.
  3. Open the correlated trace from the event context.
  4. Confirm that the same execution contains execute_tool get_forecast and its Open-Meteo fetch.

Use Agents for the agent-native session replay and trace waterfall. Use the consolidated Observability view when you need agent spans, Worker invocations, subrequests, and application logs in one operational investigation.

Verification gate

Prove it works

  • The deployed forecast smoke test completes successfully.
  • Session replay shows the model and tools in execution order.
  • The trace has an invoke_agent root with chat and execute_tool spans.
  • The model span reports token usage.
  • The forecast tool contains the outbound Open-Meteo request.
  • The consolidated Observability view contains the structured forecast event for the same execution.
Optional mini quizCheck the execution story3 questions instant feedback
1Which span represents the complete agent response?
2Which view is best for reading the conversation and tool calls in order?
3Where does the structured forecast event belong?
Recovery lane

Get back on track in under a minute

If a dev server is running, stop it first. Uncommitted work is stashed and the current commit gets a backup branch before anything moves.

Option 01

Restart this exercise

Return to cp/5-sandbox, rebuild this exercise, then use its Live deploy verification tab. Stop any running dev server with Ctrl+C before switching.

Terminal
git stash push --include-untracked -m "workshop recovery"
git fetch origin
git branch -f workshop-backup HEAD
git switch --no-track -C workshop origin/cp/5-sandbox
npm ci
Option 02

Catch up with the room

Jump to cp/6-iterate-observe, then use this checkpoint's Live deploy verification tab. Stop any running dev server with Ctrl+C before switching.

The catch-up commands retain the agent from cp/5-sandbox, keeping the on-demand workspace optimization in the bonus assignment.

Terminal
git stash push --include-untracked -m "workshop recovery"
git fetch origin
git branch -f workshop-backup HEAD
git switch --no-track -C workshop origin/cp/6-iterate-observe
git restore --source=origin/cp/5-sandbox -- src/agents/field-trip.ts
npm ci

The completed upstream branch already enables traces. The catch-up commands keep the checkpoint-5 agent so the on-demand workspace change remains a bonus exercise.

References: Cloudflare Agent tracing, Flue Cloudflare observability.

Next: Finish and verify the complete agent →