Course contentsShow
← AI Engineering
Lesson 1226 of 1,88630. Monitoring and ObservabilityFree lesson

Adding Custom Attributes to Spans

Enriching traces with LLM-specific metadata like model name, token count, temperature, and cost.

Adding Custom Attributes to Spans

What you'll learn: How to enrich distributed traces with LLM-specific metadata that makes debugging and cost analysis dramatically easier.

The Power of Rich Spans

You've learned how to trace multi-step LLM chains with OpenTelemetry, creating spans that capture each operation. But generic spans only tell you what happened and when—not the critical details that matter for AI applications. Custom attributes transform spans from simple timing records into rich diagnostic tools.

Why LLM-Specific Metadata Matters

When a trace shows your LLM call took 2.3 seconds, that's useful. But when it also shows you used gpt-4, generated 487 tokens at temperature 0.7, and cost $0.023, you can now:

  • Debug intelligently: Did high token counts cause slowness?
  • Optimize costs: Which requests burn through your budget?
  • Track configuration: What temperature setting performed best?
  • Attribute usage: Which features drive API costs?

Adding Custom Attributes

Most tracing libraries let you attach key-value metadata to spans. For LLM calls, capture:

span.set_attribute("llm.model", "gpt-4")
span.set_attribute("llm.prompt_tokens", 150)
span.set_attribute("llm.completion_tokens", 487)
span.set_attribute("llm.temperature", 0.7)
span.set_attribute("llm.estimated_cost", 0.023)
span.set_attribute("llm.provider", "openai")

Think of attributes like tags on a photo—they don't change what happened, but they make it searchable and meaningful. You can later query: "Show me all traces where cost exceeded $1" or "Find calls with token counts over 2000."

Key Takeaway: Custom span attributes transform generic traces into LLM-aware diagnostic tools by capturing model parameters, token counts, costs, and configuration—enabling targeted analysis and optimization.