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

# OpenAI SDK

> Use Raptor with the official OpenAI SDK

# OpenAI Integration

Works with both Python and TypeScript. Your existing code stays the same—just change the base URL.

## Setup

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI(
        api_key="sk-your-openai-key",
        base_url="https://proxy.raptordata.dev/v1",
        default_headers={
            "X-Raptor-Api-Key": "rpt_your-key",
            "X-Raptor-Workspace-Id": "your-workspace-id"
        }
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import OpenAI from 'openai';

    const client = new OpenAI({
      apiKey: 'sk-your-openai-key',
      baseURL: 'https://proxy.raptordata.dev/v1',
      defaultHeaders: {
        'X-Raptor-Api-Key': 'rpt_your-key',
        'X-Raptor-Workspace-Id': 'your-workspace-id'
      }
    });
    ```
  </Tab>
</Tabs>

## Chat Completions

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const response = await client.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: 'Hello!' }]
    });
    console.log(response.choices[0].message.content);
    ```
  </Tab>
</Tabs>

## Streaming

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    stream = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Tell me a story"}],
        stream=True
    )

    for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const stream = await client.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: 'Tell me a story' }],
      stream: true
    });

    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content || '');
    }
    ```
  </Tab>
</Tabs>

## Tools / Function Calling

Works exactly like the official SDK:

```python theme={null}
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get weather for a location",
        "parameters": {
            "type": "object",
            "properties": {"location": {"type": "string"}},
            "required": ["location"]
        }
    }
}]

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Weather in Paris?"}],
    tools=tools
)
```

## Embeddings

```python theme={null}
response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Hello world"
)
embedding = response.data[0].embedding
```

## Check Cache Status

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    with client.with_raw_response.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello"}]
    ) as response:
        print(f"Cache: {response.headers.get('x-raptor-cache')}")
        print(f"Latency: {response.headers.get('x-raptor-latency-ms')}ms")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    const { data, response } = await client.chat.completions.create({
      model: 'gpt-4',
      messages: [{ role: 'user', content: 'Hello' }]
    }).withResponse();

    console.log(`Cache: ${response.headers.get('x-raptor-cache')}`);
    ```
  </Tab>
</Tabs>

## Response Headers

| Header                         | Description             |
| ------------------------------ | ----------------------- |
| `X-Raptor-Cache`               | `hit` or `miss`         |
| `X-Raptor-Latency-Ms`          | Total Raptor overhead   |
| `X-Raptor-Upstream-Latency-Ms` | Time waiting for OpenAI |
| `X-Raptor-Request-Id`          | Unique ID for debugging |

## Error Handling

Errors from OpenAI pass through unchanged. Raptor adds firewall blocks:

```python theme={null}
from openai import APIError

try:
    response = client.chat.completions.create(...)
except APIError as e:
    if e.status_code == 403 and 'blocked_by_firewall' in str(e):
        print("Blocked by Raptor firewall")
    else:
        raise
```

<Note>
  All OpenAI features work: GPT-4, GPT-4o, o1, embeddings, images, audio, and more. If it works with OpenAI, it works through Raptor.
</Note>
