Python SDK
The Foil Python SDK provides automatic OpenAI instrumentation, tracing, logging, and feedback collection for Python applications.Installation
Requirements: Python 3.8 or higherConfiguration
Get your API key from the Foil Dashboard under Settings > API Keys.Configuration Options
Quick Start: OpenAI Wrapper
The easiest way to get started is with the OpenAI wrapper, which automatically traces all your API calls:wrapped_client is automatically logged to Foil with model name, input messages, output response, token usage, latency, and errors (if any).
Streaming
Streaming responses work seamlessly:Tool Calls
Function/tool calls are automatically tracked:Using Traces
For structured tracing with full span hierarchy, useFoil with agent_name and the trace() method:
Available Span Methods
Fire-and-Forget Logging
For manual logging without the OpenAI wrapper:Complete Example
Error Handling
Errors are automatically captured:Advanced Patterns
AsyncOpenAI support
AsyncOpenAI support
The wrapper works with the async OpenAI client:
Multi-turn conversations
Multi-turn conversations
Each call through the wrapped client is logged separately:
Multi-agent handoffs with create_child_context
Multi-agent handoffs with create_child_context
When one agent delegates to another, use
span.create_child_context() to create nested agent spans:Guardrails tracing
Guardrails tracing
Trace input and output guardrail checks as separate spans: