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

# Bring Your own Agents

> You can also use Prisma AIRS AI Gateway if you are doing custom agent orchestration!

## Getting Started

### 1. Install the required packages:

```sh theme={"system"}
pip install openai
```

### **2.**  Configure your OpenAI object:

```py theme={"system"}
client = OpenAI(
    api_key="PORTKEY_API_KEY",
    base_url="https://aigw.portkey.ai/v1",
    default_headers={"x-portkey-provider": "@openai-latest-a4a53d"}
)
```

## Integrate the AI Gateway with your custom Agents

This notebook demonstrates integrating a ReAct agent with the AI Gateway

## Make your agents Production-ready with AI Gateway

The AI Gateway makes your custom agents reliable, robust, and production-grade with its observability suite. Seamlessly integrate 3,000+ LLMs with your custom agents, implement fallbacks, gain granular insights into agent performance and costs, and continuously optimize your AI operations—all with just 2 lines of code.

Let's dive deep! Let's go through each of the use cases!

### 1. [Interoperability](/docs/aigw/product/ai-gateway/universal-api)

Easily switch between 3,000+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `provider ` and `API key` in the `ChatOpenAI` object.

<Tabs>
  <Tab title="OpenAI to Azure OpenAI">
    If you are using OpenAI with CrewAI, your code would look like this:

    ```py theme={"system"}
    client = OpenAI(
        api_key="PORTKEY_API_KEY",
        base_url="https://aigw.portkey.ai/v1",
        default_headers={"x-portkey-provider": "openai"}
    )
    ```

    To switch to Azure as your provider, just create a new Azure integration ([here's how](/docs/integrations/llms/azure-openai)) and use the Azure OpenAI provider.

    ```py theme={"system"}
    client = OpenAI(
        api_key="PORTKEY_API_KEY",
        base_url="https://aigw.portkey.ai/v1",
        default_headers={"x-portkey-provider": "@AZURE_PROVIDER"}
    )
    ```
  </Tab>

  <Tab title="Anthropic to AWS Bedrock">
    If you are using Anthropic with CrewAI, your code would look like this:

    ```py theme={"system"}
    client = OpenAI(
        api_key="PORTKEY_API_KEY",
        base_url="https://aigw.portkey.ai/v1",
        default_headers={"x-portkey-provider": "anthropic"}
    )
    ```

    To switch to AWS Bedrock as your provider, just create a new AWS Bedrock integration ([here's how](/docs/integrations/llms/aws-bedrock)) and use the AWS Bedrock provider.

    ```py theme={"system"}
    client = OpenAI(
        api_key="PORTKEY_API_KEY",
        base_url="https://aigw.portkey.ai/v1",
        default_headers={"x-portkey-provider": "@AWS_PROVIDER"}
    )
    ```
  </Tab>
</Tabs>

### 2. [Reliability](/docs/aigw/product/ai-gateway)

Agents are *brittle*. Long agentic pipelines with multiple steps can fail at any stage, disrupting the entire process. The AI Gateway solves this by offering built-in **fallbacks** between different LLMs or providers, **load-balancing** across multiple instances or API keys, and implementing automatic **retries** and request **timeouts**. This makes your agents more reliable and resilient.

Here's how you can implement these features using the AI Gateway's config

```py theme={"system"}
{
  "retry": {
    "attempts": 5
  },
  "strategy": {
    "mode": "loadbalance" // Choose between "loadbalance" or "fallback"
  },
  "targets": [
    {
      "provider": "openai",
      "api_key": "OpenAI_API_Key"
    },
    {
      "provider": "anthropic",
      "api_key": "Anthropic_API_Key"
    }
  ]
}
```

### 3. [Metrics](/docs/aigw/product/observability)

Agent runs can be costly. Tracking agent metrics is crucial for understanding the performance and reliability of your AI agents. Metrics help identify issues, optimize runs, and ensure that your agents meet their intended goals.

The AI Gateway automatically logs comprehensive metrics for your AI agents, including **cost**, **tokens used**, **latency**, etc. Whether you need a broad overview or granular insights into your agent runs, the AI Gateway's customizable filters provide the metrics you need. For agent-specific observability, add `Trace-id` to the request headers for each agent.

```py theme={"system"}
llm2 = ChatOpenAI(
    api_key="PORTKEY_API_KEY",
    base_url="https://aigw.portkey.ai/v1",
    default_headers={"x-portkey-provider": "@anthropic", "x-portkey-trace-id": "research_agent1"}
)
```

### 4. [Logs](/docs/aigw/product/observability/logs)

Agent runs are complex. Logs are essential for diagnosing issues, understanding agent behavior, and improving performance. They provide a detailed record of agent activities and tool use, which is crucial for debugging and optimizing processes.

The AI Gateway offers comprehensive logging features that capture detailed information about every action and decision made by your AI agents. Access a dedicated section to view records of agent executions, including parameters, outcomes, function calls, and errors. Filter logs based on multiple parameters such as trace ID, model, tokens used, and metadata.

### 5. [Continuous Improvement](/docs/aigw/product/observability/feedback)

Improve your Agent runs by capturing qualitative & quantitative user feedback on your requests. The AI Gateway's Feedback APIs provide a simple way to get weighted feedback from customers on any request you served, at any stage in your app. You can capture this feedback on a request or conversation level and analyze it by adding meta data to the relevant request.

### 6. [Caching](/docs/aigw/product/ai-gateway/cache-simple-and-semantic)

Agent runs are time-consuming and expensive due to their complex pipelines. Caching can significantly reduce these costs by storing frequently used data and responses. The AI Gateway offers a built-in caching system that stores past responses, reducing the need for agent calls saving both time and money.

```py theme={"system"}
{
 "cache": {
    "mode": "semantic" // Choose between "simple" or "semantic"
 }
}
```

### 7.[ Security & Compliance](/docs/aigw/product/enterprise-offering/security)

Set budget limits on provider API keys and implement fine-grained user roles and permissions for both the app and the AI Gateway APIs.

***

## [AI Gateway Config](/docs/aigw/product/ai-gateway/configs)

Many of these features are driven by the AI Gateway's Config architecture. Strata Cloud Manager simplifies creating, managing, and versioning your Configs.

For more information on using these features and setting up your Config, please refer to the AI Gateway documentation.


## Related topics

- [Bring Your Own Auth](/docs/aigw/product/mcp-gateway/authentication/external-oauth.md)
- [Bring Your Own Guardrails](/docs/aigw/integrations/guardrails/bring-your-own-guardrails.md)
- [Bring Your Own LLM](/docs/aigw/integrations/llms/byollm.md)
- [Guardrails](/docs/aigw/product/guardrails.md)
- [List of Guardrail Checks](/docs/aigw/product/guardrails/list-of-guardrail-checks.md)
