> ## 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.

# OpenAI Agent Builder (Python)

> Add Prisma AIRS AI Gateway to visual agent workflows exported from OpenAI Agent Builder.

OpenAI Agent Builder is a visual canvas for creating multi-step agent workflows. Export production-ready Python code and add the AI Gateway for:

* Complete observability of agent workflows
* Cost tracking and optimization
* Reliability features (fallbacks, retries)
* Access to 3,000+ LLMs
* Guardrails for safe agent behavior

## Quick Start

<Steps>
  <Step title="Design in Agent Builder">
    Open [OpenAI Agent Builder](https://platform.openai.com/playground/agent-builder) and create your workflow using the visual canvas.
  </Step>

  <Step title="Export Code">
    Click **Code** → **Agents SDK** to get the Python implementation.
  </Step>

  <Step title="Install Packages">
    ```bash theme={"system"}
    pip install -U openai-agents
    ```
  </Step>

  <Step title="Add the AI Gateway">
    Replace the OpenAI client with the AI Gateway:

    ```python theme={"system"}
    from agents import Agent, Runner, set_default_openai_client, set_default_openai_api
    from openai import AsyncOpenAI

    client = AsyncOpenAI(
        base_url="https://aigw.portkey.ai/v1",
        api_key="YOUR_PORTKEY_API_KEY",
        default_headers={"x-portkey-provider": "@openai-prod"}
    )
    set_default_openai_client(client, use_for_tracing=False)
    set_default_openai_api("chat_completions")

    # Your Agent Builder workflow code continues as exported...
    ```

    <Note>
      Update the `model` field to use the AI Gateway's format: `@openai-prod/gpt-4o`
    </Note>
  </Step>
</Steps>

## Setup

<Steps>
  <Step title="Add Provider in Model Catalog">
    Go to [Model Catalog → Add Provider](https://stratacloudmanager.paloaltonetworks.com/). Select your provider (OpenAI, Anthropic, etc.), enter API keys, and name it (e.g., `openai-prod`).

    Your provider slug is `@openai-prod`.
  </Step>

  <Step title="Get AI Gateway API Key">
    Create an API key at [stratacloudmanager.paloaltonetworks.com](https://stratacloudmanager.paloaltonetworks.com/).

    **Pro tip:** Attach a [config](https://stratacloudmanager.paloaltonetworks.com/) for fallbacks, caching, and guardrails—applies automatically.
  </Step>
</Steps>

## Production Features

### Observability

All workflow executions are logged:

Add trace IDs and metadata for filtering:

```python theme={"system"}
import json
default_headers={
    "x-portkey-provider": "@openai-prod",
    "x-portkey-trace-id": "workflow-session-123",
    "x-portkey-metadata": json.dumps({
        "workflow_type": "research",
        "_user": "user_123",
        "environment": "production"
    }),
}
```

### Reliability

Enable fallbacks via [Configs](https://stratacloudmanager.paloaltonetworks.com/):

```python theme={"system"}
import json
client = AsyncOpenAI(
    base_url="https://aigw.portkey.ai/v1",
    api_key="YOUR_PORTKEY_API_KEY",
    default_headers={
        "x-portkey-config": json.dumps({
            "strategy": { "mode": "fallback" },
            "targets": [
                { "override_params": { "model": "@openai-prod/gpt-4o" } },
                { "override_params": { "model": "@anthropic-prod/claude-sonnet-4" } }
            ]
        }),
    }
)
```

<CardGroup cols="2">
  <Card title="Automatic Retries" icon="rotate" href="/docs/aigw/product/ai-gateway/automatic-retries">
    Handles temporary failures automatically
  </Card>

  <Card title="Load Balancing" icon="scale-balanced" href="/docs/aigw/product/ai-gateway/load-balancing">
    Distribute across multiple keys
  </Card>

  <Card title="Conditional Routing" icon="route" href="/docs/aigw/product/ai-gateway/conditional-routing">
    Route based on request attributes
  </Card>

  <Card title="Fallbacks" icon="shield" href="/docs/aigw/product/ai-gateway/fallbacks">
    Switch to backup providers automatically
  </Card>
</CardGroup>

### Guardrails

Add input/output validation:

```python theme={"system"}
import json
default_headers={
    "x-portkey-provider": "@openai-prod",
    "x-portkey-config": json.dumps({
        "input_guardrails": ["guardrail-id-xxx"],
        "output_guardrails": ["guardrail-id-yyy"]
    }),
}
```

Guardrails can:

* Detect and redact PII
* Filter harmful content
* Validate response formats
* Apply custom business rules

<Card title="Guardrails Guide" icon="shield-check" href="/docs/aigw/product/guardrails">
  PII detection, content filtering, and custom rules
</Card>

### Caching

Reduce costs with response caching:

```python theme={"system"}
import json
default_headers={
    "x-portkey-provider": "@openai-prod",
    "x-portkey-config": json.dumps({ "cache": { "mode": "semantic" } }),
}
```

### Prompt Templates

Use the AI Gateway's prompt management for versioned prompts:

## Switching Providers

Use any of 3,000+ models:

```python theme={"system"}
# Prefix the model with the provider slug to pick the integration.
model="@openai-prod/gpt-4o"
model="@anthropic-prod/claude-sonnet-4-20250514"
model="@google-prod/gemini-2.0-flash"
```

<Card title="Supported Providers" icon="server" href="/docs/aigw/integrations/llms">
  See all 3,000+ supported models
</Card>

## Enterprise Governance

Set up centralized control for your workflows.

<Steps>
  <Step title="Add Provider with Budget">
    Go to [Model Catalog](https://stratacloudmanager.paloaltonetworks.com/) → Add Provider. Set budget limits and rate limits.
  </Step>

  <Step title="Create Config">
    Go to [Configs](https://stratacloudmanager.paloaltonetworks.com/):

    ```json theme={"system"}
    {
      "override_params": { "model": "@openai-prod/gpt-4o" }
    }
    ```
  </Step>

  <Step title="Create Team API Keys">
    Go to [API Keys](https://stratacloudmanager.paloaltonetworks.com/). Create keys per team, attach configs.
  </Step>

  <Step title="Distribute to Teams">
    Teams use their AI Gateway API key:

    ```python theme={"system"}
    client = AsyncOpenAI(
        base_url="https://aigw.portkey.ai/v1",
        api_key="TEAM_PORTKEY_API_KEY"  # Config attached to key
    )
    ```
  </Step>
</Steps>

<Card title="Governance & Administration" icon="building" href="/docs/aigw/introduction/feature-overview#governance-%26-administration">
  Access control, encryption, and audit trails
</Card>

## FAQ

<AccordionGroup>
  <Accordion title="Can I use the AI Gateway with existing Agent Builder workflows?">
    Yes. Export your workflow, add AI Gateway client initialization, and your code works unchanged.
  </Accordion>

  <Accordion title="Does the AI Gateway work with all Agent Builder features?">
    Yes. Handoffs, tools, guardrails—all work with the AI Gateway observability and reliability.
  </Accordion>

  <Accordion title="How do I track workflow costs?">
    Add metadata to your requests. Filter by workflow type, user, or environment in the dashboard.
  </Accordion>

  <Accordion title="Can I use my own API keys?">
    Yes. The AI Gateway stores your provider keys securely. Rotate keys without code changes.
  </Accordion>
</AccordionGroup>

## Resources

<CardGroup cols="2">
  <Card title="OpenAI Agents SDK" icon="robot" href="/docs/aigw/integrations/agents/openai-agents">
    Full SDK integration guide
  </Card>

  <Card title="Configs" icon="gear" href="/docs/aigw/product/ai-gateway/configs">
    Fallbacks, caching, and routing
  </Card>

  <Card title="OpenAI Agents Docs" icon="book" href="https://openai.github.io/openai-agents-python/">
    Official documentation
  </Card>
</CardGroup>


## Related topics

- [Overview](/docs/aigw/integrations/libraries.md)
- [OpenAI Agent Builder (TypeScript)](/docs/aigw/integrations/libraries/openai-agent-builder.md)
- [OpenAI Agents SDK (Python)](/docs/aigw/integrations/agents/openai-agents.md)
- [OpenAI Agents SDK (TypeScript)](/docs/aigw/integrations/agents/openai-agents-ts.md)
- [Strict OpenAI Compliance](/docs/aigw/product/ai-gateway/strict-open-ai-compliance.md)
