Prerequisites
Install dependencies:Sample Integration
quotient_trace_openai_agents.py
Sample Integration for Streaming
For agents that are wrapped in a backend API framework (e.g. FastAPI), async + streaming responses are handled differently than in a standalone agent application in that tokens are streamed via an async Python generator. When this is the case, you need to use theuse_span context manager to ensure that all operations within the context are part of the same trace.
Below you can find an example of how to do this with an OpenAI Agent in a FastAPI streaming application.
Key Components:
- Root Span:
quotient.tracer.start_span()creates the main span that will contain all child operations - Context Management:
use_span(root_span, end_on_exit=False)makes the span current for all operations within the context - Streaming: The generator function handles the streaming response while maintaining trace context
- Cleanup:
root_span.end()andquotient.force_flush()ensure traces are properly closed and sent
Notes
- The instrumentor emits spans for tool calls, actions, and final responses so you can replay the agent’s reasoning.
- Use
asyncio.run(as shown) or integrate directly into your existing event loop.
Next: LangGraph