Interview question
Instrument an AI request trace without leaking prompts or customer data
Instruments an AI workflow with correlated stage spans, low-cardinality versions, usage and outcome signals, and explicit content-capture controls.
TL;DR
Instruments an AI workflow with correlated stage spans, low-cardinality versions, usage and outcome signals, and explicit content-capture controls.
Trace boundaries, semantic attributes, cardinality control, privacy-safe diagnostics, sampling, stage correlation, and operational queries.
Practice the problem like a real interview: restate, reason, implement, and test.
Instrument a request that performs retrieval, generation, validation, and optional repair. Operators must locate slow or failing stages and compare versions, but prompts, retrieved text, generated content, user IDs, and document titles are forbidden from default telemetry. Add spans, metrics, and structured outcomes that support latency, cost, refusal, validation, and quality investigations.
Trace: ai.answer -> ai.retrieve -> ai.generate -> ai.validate -> ai.repair
Safe attributes: operation, route ID, model version, prompt version, schema version, token counts, result code
Forbidden by default: prompt text, response text, chunk text, email, tenant name, raw document ID.
async def answer(request: AnswerRequest) -> AnswerResult:
with tracer.start_as_current_span("ai.answer") as root:
root.set_attributes({
"gen_ai.operation.name": "answer",
"app.ai.route.id": request.route_id,
"app.ai.prompt.version": PROMPT_VERSION,
"app.ai.schema.version": SCHEMA_VERSION,
})
with tracer.start_as_current_span("ai.retrieve") as span:
chunks = await retrieve(request.safe_scope, request.query)
span.set_attribute("app.ai.retrieved.count", len(chunks))
with tracer.start_as_current_span("ai.generate") as span:
generated = await gateway.generate(request.query, chunks)
span.set_attributes({
"gen_ai.request.model": generated.model_id,
"gen_ai.usage.input_tokens": generated.input_tokens,
"gen_ai.usage.output_tokens": generated.output_tokens,
"app.ai.outcome": generated.safe_outcome,
})
with tracer.start_as_current_span("ai.validate") as span:
result = validate(generated, chunks)
span.set_attribute("app.ai.validation.result", result.safe_code)
return result
# Exporter policy rejects prompt, response, chunk text, email, and raw customer IDs.
Instrumentation adds O(number of workflow stages) records and constant per-stage metadata. Export volume and high-cardinality dimensions, not algorithmic computation, are the main scaling risks.
Next in AI Production Scenarios & Labs: Bounded approved tool workflow