> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic Logfire

> Modern Python observability with automatic OpenAI instrumentation and intelligent gateway routing

[Pydantic Logfire](https://pydantic.dev/logfire) is a modern observability platform from the creators of Pydantic, designed specifically for Python applications. It provides automatic instrumentation for popular libraries including OpenAI, Anthropic, and other LLM providers, making it an excellent choice for AI application monitoring.

> [!NOTE]
>
> Logfire's automatic instrumentation combined with Portkey's intelligent gateway creates a powerful observability stack where every trace is enriched with routing decisions, cache performance, and cost optimization data.

## Why Logfire + Portkey?

> [card] Zero-Code OpenAI Instrumentation
>
> Logfire automatically instruments OpenAI SDK calls without any code changes

> [card] Gateway Intelligence
>
> Portkey adds routing context, fallback decisions, and cache performance to every trace

> [card] Python-First Design
>
> Built by the Pydantic team specifically for Python developers

> [card] Real-Time Insights
>
> See traces immediately with actionable optimization opportunities

## Quick Start

### Prerequisites

* Python
* Portkey account with API key
* OpenAI API key (or add it to [Model Catalog](https://docs.portkey.ai/docs/product/model-catalog.md))

### Step 1: Install Dependencies

Install the required packages for Logfire and Portkey integration:

```bash theme={"system"}
pip install logfire openai portkey-ai
```

### Step 2: Basic Setup - Send Traces to Portkey

First, let's configure Logfire to send traces to Portkey's OpenTelemetry endpoint:

```python theme={"system"}
import os
import logfire

# Configure OpenTelemetry export to Portkey
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY"

# Initialize Logfire
logfire.configure(
    service_name='my-llm-app',
    send_to_logfire=False,  # Disable sending to Logfire cloud
)

# Instrument OpenAI globally
logfire.instrument_openai()
```

### Step 3: Complete Setup - Use Portkey's Gateway

For the best experience, route your LLM calls through Portkey's gateway to get automatic optimizations:

```python theme={"system"}
import logfire
import os
from portkey_ai import createHeaders
from openai import OpenAI

# Configure OpenTelemetry export
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.portkey.ai/v1/logs/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "x-portkey-api-key=YOUR_PORTKEY_API_KEY"

# Initialize Logfire
logfire.configure(
    service_name='my-llm-app',
    send_to_logfire=False,
)

# Create OpenAI client with Portkey's gateway
client = OpenAI(
    api_key="PORTKEY_API_KEY",
    base_url="https://api.portkey.ai/v1",
    default_headers=createHeaders(
        api_key="PORTKEY_API_KEY",
        provider="@openai-prod"  # Your AI Provider slug from Model Catalog
    )
)

# Instrument the Portkey-configured client
logfire.instrument_openai(client)
```

### Step 4: Make Instrumented LLM Calls

Now your LLM calls are automatically traced by Logfire and enhanced by Portkey:

```python theme={"system"}
# Simple chat completion - automatically traced
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Explain the benefits of observability in LLM applications"}],
    temperature=0.7
)

print(response.choices[0].message.content)
```

## Next Steps

> [card] [Configure Gateway](https://docs.portkey.ai/docs/product/ai-gateway/configs.md)
>
> Set up intelligent routing, fallbacks, and caching

> [card] [Model Catalog](https://docs.portkey.ai/docs/product/model-catalog.md)
>
> Manage AI providers, credentials, and model access centrally

> [card] [View Analytics](https://docs.portkey.ai/docs/product/observability/analytics.md)
>
> Analyze costs, performance, and usage patterns

> [card] [Set Up Budget & Rate Limts](https://docs.portkey.ai/docs/product/administration/enforce-budget-and-rate-limit.md)
>
> Set Rate and Budget Limits per model/user/api-key

***

## See Your Traces in Action

Once configured, navigate to the [Portkey dashboard](https://app.portkey.ai/logs) to see your Logfire instrumentation combined with gateway intelligence:

![OpenTelemetry traces in Portkey](https://mintcdn.com/portkey-docs/Buc1Vm2P31GSPm3S/images/product/opentelemetry.png?fit=max\&auto=format\&n=Buc1Vm2P31GSPm3S\&q=85\&s=bc982b581d5ce60764d207b004b4677f)

***

> [card] [Portkey is now PRISMA AIRS AI Gateway. See it in action.](https://www.paloaltonetworks.in/ai-security/ai-gateway?utm_source=portkey\&utm_medium=referral\&utm_campaign=prisma_airs\&utm_content=docs_nav#contact)
>
> Contact Us


## Related topics

- [Supported OTel Libraries](https://docs.portkey.ai/docs/product/observability/opentelemetry/list-of-supported-otel-instrumenters.md)
- [Pydantic AI](https://docs.portkey.ai/docs/integrations/agents/pydantic-ai.md)
- [OpenAI Agents SDK (Python)](https://docs.portkey.ai/docs/integrations/agents/openai-agents.md)
- [Controlled Generations](https://docs.portkey.ai/docs/integrations/llms/vertex-ai/controlled-generations.md)
- [Structured Outputs](https://docs.portkey.ai/docs/integrations/llms/openai/structured-outputs.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.