AIF-47Recommended
Log every model call
Write one structured log line per model call. It holds the feature, request ID, prompt ID and version, model version, tokens, cost, latency, ending and outcome.
How to do it
Write the line in the model gateway, so no call is missed. Use the same request ID as the rest of the request, as in tie every line to its request. Leave the prompt and answer text out of this line. They go to a separate store with its own limits, as the next rule shows. Return a call ID to the feature, so feedback can point at the exact call. Also emit each call as a trace span under the request's span, with the OpenTelemetry names for generative AI. Examples are gen_ai.request.model, gen_ai.usage.input_tokens and gen_ai.usage.output_tokens. Those conventions are still marked Development, so record the version you follow. Agent runs add their own spans, as record every step of every run shows.
Examples
JSON example
{"time":"2026-10-20T09:15:04.120Z","level":"info","event":"ai.call","request_id":"7f3c2a9e","call_id":"c_81f2","feature":"return-assistant","prompt_id":"return-assistant","prompt_version":7,"model":"example-model-2026-05-14","input_tokens":1840,"cached_input_tokens":1536,"output_tokens":212,"cost_usd":0.0013,"latency_ms":1420,"ending":"complete","outcome":"shown"}
TypeScript example
log.info({ event: 'ai.call', requestId, callId, feature, promptId: prompt.id, promptVersion: prompt.version, model: response.model,
inputTokens: usage.inputTokens, cachedInputTokens: usage.cachedInputTokens, outputTokens: usage.outputTokens,
costUsd: callCostUsd(response.model, usage), latencyMs, ending, outcome });
Python example
log.info("ai.call", request_id=request_id, call_id=call_id, feature=feature, prompt_id=prompt.id, prompt_version=prompt.version,
model=response.model, input_tokens=usage.input_tokens, cached_input_tokens=usage.cached_input_tokens,
output_tokens=usage.output_tokens, cost_usd=call_cost_usd(response.model, usage),
latency_ms=latency_ms, ending=ending, outcome=outcome)
How to check
Parse one hour of gateway logs with a JSON tool. Every ai.call line has all the listed fields, and none contains a field holding prompt or answer text. The trace of a test request shows the model call span under the request span, with the model name and token counts.
Sources
Related rules
- AIF-9 Log which prompt made each answer in AI features
- AIF-4 Call models through one module in AI features
- OPS-7 Tie every line to its request in Operations and observability
- AIF-54 Record every step of every run in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/logs-privacy-and-feedback/log-model-calls">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.