> ## 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 (TypeScript)

> 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 TypeScript 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 TypeScript implementation.
  </Step>

  <Step title="Install Packages">
    ```bash theme={"system"}
    npm install @openai/agents openai
    ```
  </Step>

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

    ```typescript theme={"system"}
    import { Agent, run, setDefaultOpenAIClient, setOpenAIAPI } from '@openai/agents';
    import { OpenAI } from 'openai';

    const client = new OpenAI({
        baseURL: "https://aigw.portkey.ai/v1",
        apiKey: 'YOUR_PORTKEY_API_KEY',
        defaultHeaders: { "x-portkey-provider": '@openai-prod' }
    });
    setDefaultOpenAIClient(client);
    setOpenAIAPI('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:

```typescript theme={"system"}
defaultHeaders: {
    "x-portkey-provider": '@openai-prod',
    "x-portkey-trace-id": 'workflow-session-123',
    "x-portkey-metadata": JSON.stringify({
        workflow_type: 'research',
        _user: 'user_123',
        environment: 'production'
    }),
}
```

### Reliability

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

```typescript theme={"system"}
const client = new OpenAI({
    baseURL: "https://aigw.portkey.ai/v1",
    apiKey: 'YOUR_PORTKEY_API_KEY',
    defaultHeaders: {
        "x-portkey-config": JSON.stringify({
            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:

```typescript theme={"system"}
defaultHeaders: {
    "x-portkey-provider": '@openai-prod',
    "x-portkey-config": JSON.stringify({
        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:

```typescript theme={"system"}
defaultHeaders: {
    "x-portkey-provider": '@openai-prod',
    "x-portkey-config": JSON.stringify({ cache: { mode: 'semantic' } }),
}
```

### Prompt Templates

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

## Switching Providers

Use any of 3,000+ models:

```typescript 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:

    ```typescript theme={"system"}
    const client = new OpenAI({
        baseURL: "https://aigw.portkey.ai/v1",
        apiKey: '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-ts">
    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-js/">
    Official documentation
  </Card>
</CardGroup>


## Related topics

- [OpenAI Agents SDK (TypeScript)](/docs/aigw/integrations/agents/openai-agents-ts.md)
- [OpenAI Agent Builder (Python)](/docs/aigw/integrations/libraries/openai-agent-builder-python.md)
- [AWS AgentCore](/docs/aigw/integrations/agents/agentcore.md)
- [Agentic Usage](/docs/aigw/api-reference/inference-api/agentic-usage.md)
- [Mastra Agents](/docs/aigw/integrations/agents/mastra-agents.md)
