Context Managers - Flexible Tracing
Understanding context managers and how they provide flexible, granular control over tracing in your AI applications
Context managers are Python's powerful with statement syntax that provides the most flexible way to trace specific parts of your code. They give you granular control over what to trace, when to trace it, and how to add metadataâall without requiring decorators or modifying function signatures.
đŻ What are Context Managers?
Context managers in Noveum provide:
- Granular control - Trace only specific parts of functions
- No decorators required - Add tracing without modifying function signatures
- Flexible metadata - Add attributes and events dynamically
- Automatic cleanup - Spans are properly closed even if errors occur
- Nested tracing - Easily create parent-child span relationships
đď¸ Basic Syntax
The basic pattern for using context managers:
đ§ Core Context Managers
trace_llm_call / trace_llm
Trace LLM API calls with automatic token and cost tracking.
Parameters:
model- The AI model name (e.g., "gpt-4", "claude-3-opus")provider- The AI provider (e.g., "openai", "anthropic")operation- Optional operation name for context
trace_operation
Trace any generic operation or business logic.
Parameters:
name- Operation name (required)attributes- Optional initial attributes dictionary
trace_agent_operation / trace_agent
Trace agent operations in multi-agent workflows.
Parameters:
agent_type- Type of agent (e.g., "researcher", "writer")agent_id- Unique identifier for the agent instanceoperation- Optional operation name
trace_batch_operation
Trace batch processing operations.
Parameters:
name- Batch operation name (required)batch_size- Optional size of the batch
trace_pipeline_stage
Trace individual stages in data pipelines.
Parameters:
name- Stage name (required)stage_number- Optional stage position in pipeline
trace_function_calls
Trace function calls with granular control.
Parameters:
name- Function name (required)
create_child_span
Explicitly create child spans for nested operations.
Parameters:
name- Child span name (required)parent_span- Parent span object (required)
đ Streaming Context Managers
streaming_llm
Handle streaming LLM responses with automatic token tracking.
trace_streaming
Wrap any stream iterator for tracing.
đĄ Practical Examples
Multi-Step Operation with Context Managers
Error Handling with Context Managers
Nested Spans for Complex Workflows
Customer Support Bot Example
⨠Best Practices
Why Use Context Managers
Context Managers provide:
- Ability to trace only specific parts of a function
- Dynamic attributes based on runtime conditions
- Easy integration with legacy code without modifications
- Fine-grained control over span boundaries
- Automatic resource cleanup even when errors occur
Structure Nested Traces Properly
Set Attributes at the Right Time
Handle Errors Gracefully
Add Meaningful Events
đ Context Manager vs Manual Spans
Context managers provide automatic span management, but you can also create spans manually for more control:
Recommendation: Use context managers unless you need explicit control over span lifecycle.
đ Next Steps
Now that you understand context managers, explore these related concepts:
- Traces - Complete request journeys
- Spans - Individual operations within traces
- Attributes - Metadata and context
- Events - Point-in-time occurrences
Integration Examples
- Simple LLM Integration - Basic LLM tracing with context managers
- SDK Integration Guide - Complete integration patterns
Context managers are the most flexible way to add tracing to your AI applications. They provide granular control while maintaining clean, readable code with automatic resource management.
Get Early Access to Noveum.ai Platform
Be the first one to get notified when we open Noveum Platform to more users. All users get access to Observability suite for free, early users get free eval jobs and premium support for the first year.