Sub topic 14
Capturing prompts and responses safely
This chapter assumes you can create spans with the OpenTelemetry Python SDK and set attributes on them. It also assumes you have instrumented a model call as shown in earlier chapters.
Why content is a special case
Prompts and responses are the most useful data for debugging an LLM app. They are also the riskiest. They can hold names, emails, account numbers, health details, internal documents and secrets that users pasted in. A trace backend usually has broader access and longer retention than your primary database, so content that lands there can spread further than you intended.
The GenAI semantic conventions treat content as opt-in. Token counts, model names and operation names are safe to record by default. Message content is not.
Where content goes
Content can be large and sensitive, so the conventions give it a different home from small attributes. Instrumentation can record it as span attributes or as events, and newer guidance moves it to log-style events or to external storage with a reference on the span. The names and placement have changed between convention versions, so check the version your library implements before you depend on a specific attribute name.
A capture switch with redaction
The example below gates capture behind an environment variable, redacts email addresses, and truncates long text.
import os
import re
from opentelemetry import trace
tracer = trace.get_tracer("support_agent")
CAPTURE = os.getenv("CAPTURE_CONTENT", "false").lower() == "true"
MAX_CHARS = 2000
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
def clean(text):
text = EMAIL.sub("[email]", text)
return text[:MAX_CHARS]
def traced_chat(prompt, model="example-model"):
with tracer.start_as_current_span(f"chat {model}") as span:
span.set_attribute("gen_ai.operation.name", "chat")
span.set_attribute("gen_ai.request.model", model)
reply = "Contact me at [email protected]" # stand-in for a model call
if CAPTURE:
span.add_event("prompt", {"content": clean(prompt)})
span.add_event("response", {"content": clean(reply)})
return replyCapture is off unless you set CAPTURE_CONTENT=true. Redaction runs before the data reaches the span, so the raw text never leaves your process.
Decision rules
- Redact in your application, not only in the Collector. Data you never export cannot leak in transit.
- Use the Collector as a second layer. Its processors can drop or hash attributes before they reach the backend.
- Capture in development and staging freely. In production, capture only for a sampled share of traffic, or only for failed requests.
- Set a size limit. Long contexts can exceed backend attribute limits and raise storage cost.
- Treat tool arguments and retrieved documents as content too. They often hold the same private data.
Common errors
- Turning on capture globally to debug one issue, then leaving it on.
- Redacting after
set_attribute. The value is already on the span. - Forgetting that exception messages can contain user input.
record_exceptionstores the message. - Assuming the backend access controls match your database controls.
Exercise
Your support agent handles billing questions. You want to debug bad answers in production without storing customer data in your trace backend. Name two settings or practices from this chapter that you would combine.
Show answer
Keep content capture off by default and enable it only for a small sampled share of traffic or for failed requests. Redact and truncate text in the application before it is set on the span, and add Collector processors as a second layer. Token counts and model names can still be recorded on every span.