Skip to content

Model-turn telemetry

Time: 5 minutes

Checkpoint 6 uses built-in agent tracing and needs no application observer. In this experiment, add a focused runtime-event subscriber that exports model tokens, duration, failures, and the provider’s gatewayLogId to Workers Logs.

Update src/app.ts to register the observer before creating the route map. The diff starts from completed checkpoint 6; Complete file retains every route:

src/app.ts+21−0

Changes from Checkpoint 6 (guide) → Model-turn telemetry bonus

src/app.ts
===================================================================
--- a/src/app.ts Checkpoint 6 (guide)
+++ b/src/app.ts Model-turn telemetry bonus
@@ -1,8 +1,29 @@
import { Hono } from 'hono';
import { createAgentRouter } from '@flue/runtime/routing';
+import { observe } from '@flue/runtime';
import { FieldTrip } from './agents/field-trip.ts';
import chatUi from './ui/index.html?raw';
+observe((event) => {
+ if (event.type !== 'turn') return;
+ const usage = event.response.usage;
+ console.log({
+ event: 'flue_model_turn',
+ agent: event.agentName ?? null,
+ conversationId: event.conversationId ?? null,
+ submissionId: event.submissionId ?? null,
+ turnId: event.turnId,
+ model: event.request.requestedModel,
+ durationMs: event.durationMs,
+ inputTokens: usage?.input ?? null,
+ outputTokens: usage?.output ?? null,
+ totalTokens: usage?.totalTokens ?? null,
+ gatewayLogId: event.response.gatewayLogId ?? null,
+ isError: event.isError,
+ error: event.response.error?.message ?? null,
+ });
+});
+
// src/app.ts is the Flue route map. Its default export owns every HTTP request
// the Worker receives. Agents get mounted here with createAgentRouter(...)
// starting in checkpoint 1.
Terminal window
npm run typecheck
npm run deploy
sleep 5
npm run smoke -- https://field-trip-agent.<subdomain>.workers.dev telemetry-live-1 "Describe what you do in one sentence."

Open Workers & Pages → field-trip-agent → Observability → Logs and filter event = flue_model_turn. Copy a non-null gatewayLogId, then locate that request in AI → AI Gateway → Logs. If the provider does not return this ID, the field remains null; it is not evidence of a failed model call.

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

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

Verification gate

Prove it works

  • Workers Logs contains an object with event: ‘flue_model_turn’.
  • The object identifies the Gemma model and reports token counts.
  • gatewayLogId correlates the turn with its AI Gateway log when available.

Remove the observe import and callback, then redeploy (or restart Vite for Local dev). Checkpoint 6’s built-in agent tracing continues to work without this observer.

Reference: Flue observability.