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 complete file below keeps every route. Changes shows the comparison with completed checkpoint 6:

  • src
    • app.ts

Replace the entire contents of this file. In your code editor (for example, VS Code), open the file named above. Copy the code in Complete file, paste it into that file's editing pane, and save.

src/app.ts
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.
const app = new Hono();
// A small chat UI for trying the agent in a browser, locally and on workers.dev.
app.get('/', (c) => c.html(chatUi));
app.get('/api/ping', (c) => c.text('pong'));
app.route('/agents/field-trip', createAgentRouter(FieldTrip));
export default app;
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.