> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/cloudflare/cloudflare-typescript/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Gateway

> Control, monitor, and optimize AI API usage

AI Gateway provides a unified interface for AI providers with caching, rate limiting, analytics, and cost controls. Use the API to create gateways and manage AI traffic.

## Overview

Access the AI Gateway API:

```typescript theme={null}
import Cloudflare from 'cloudflare';

const client = new Cloudflare({
  apiToken: process.env.CLOUDFLARE_API_TOKEN,
});

// Access AI Gateway resources
const aiGateway = client.aiGateway;
```

## Gateways

Manage AI Gateway instances.

### Create a gateway

Create a new AI Gateway.

```typescript theme={null}
const gateway = await client.aiGateway.create({
  account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
  id: 'my-gateway',
  cache_invalidate_on_update: true,
  cache_ttl: 3600,
  collect_logs: true,
  rate_limiting_interval: 60,
  rate_limiting_limit: 100,
  rate_limiting_technique: 'sliding',
});
```

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="id" type="string" required>
  Gateway identifier (used in API calls)
</ParamField>

<ParamField path="cache_ttl" type="number">
  Cache time-to-live in seconds (null to disable caching)
</ParamField>

<ParamField path="cache_invalidate_on_update" type="boolean" required>
  Invalidate cache when gateway is updated
</ParamField>

<ParamField path="collect_logs" type="boolean" required>
  Enable request/response logging
</ParamField>

<ParamField path="rate_limiting_interval" type="number">
  Rate limit window in seconds (null to disable)
</ParamField>

<ParamField path="rate_limiting_limit" type="number">
  Maximum requests per interval (null to disable)
</ParamField>

<ParamField path="rate_limiting_technique" type="string" required>
  Rate limiting algorithm: 'fixed' or 'sliding'
</ParamField>

<ResponseField name="id" type="string">
  The gateway ID
</ResponseField>

<ResponseField name="account_id" type="string">
  Your account ID
</ResponseField>

<ResponseField name="created_at" type="string">
  ISO 8601 timestamp when the gateway was created
</ResponseField>

<ResponseField name="modified_at" type="string">
  ISO 8601 timestamp when the gateway was last modified
</ResponseField>

### List gateways

Retrieve all AI Gateways in your account.

```typescript theme={null}
for await (const gateway of client.aiGateway.list({
  account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
})) {
  console.log(gateway);
}
```

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="search" type="string">
  Filter gateways by ID
</ParamField>

<ParamField path="page" type="number">
  Page number for pagination
</ParamField>

<ParamField path="per_page" type="number">
  Number of gateways per page
</ParamField>

### Get a gateway

Retrieve details about a specific gateway.

```typescript theme={null}
const gateway = await client.aiGateway.get(
  'my-gateway',
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

### Update a gateway

Update gateway configuration.

```typescript theme={null}
const gateway = await client.aiGateway.update(
  'my-gateway',
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    cache_ttl: 7200,
    rate_limiting_limit: 200,
    rate_limiting_interval: 60,
    rate_limiting_technique: 'sliding',
    cache_invalidate_on_update: true,
    collect_logs: true,
  }
);
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID to update
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

### Delete a gateway

Delete an AI Gateway.

```typescript theme={null}
await client.aiGateway.delete(
  'my-gateway',
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID to delete
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

## Logs

Manage and query AI Gateway request logs.

### List logs

Retrieve logs for a gateway.

```typescript theme={null}
for await (const log of client.aiGateway.logs.list(
  'my-gateway',
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    start_date: '2024-01-01',
    end_date: '2024-01-31',
  }
)) {
  console.log(log);
}
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="start_date" type="string">
  Start date for logs (ISO 8601)
</ParamField>

<ParamField path="end_date" type="string">
  End date for logs (ISO 8601)
</ParamField>

<ParamField path="page" type="number">
  Page number for pagination
</ParamField>

<ParamField path="per_page" type="number">
  Number of logs per page
</ParamField>

### Get a log entry

Retrieve a specific log entry.

```typescript theme={null}
const log = await client.aiGateway.logs.get(
  'my-gateway',
  logId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

<ResponseField name="id" type="string">
  The log entry ID
</ResponseField>

<ResponseField name="timestamp" type="string">
  ISO 8601 timestamp of the request
</ResponseField>

<ResponseField name="provider" type="string">
  AI provider (e.g., 'openai', 'anthropic')
</ResponseField>

<ResponseField name="model" type="string">
  Model used for the request
</ResponseField>

<ResponseField name="status_code" type="number">
  HTTP status code
</ResponseField>

<ResponseField name="cached" type="boolean">
  Whether the response was served from cache
</ResponseField>

<ResponseField name="duration_ms" type="number">
  Request duration in milliseconds
</ResponseField>

### Get log request

Retrieve the request payload for a log entry.

```typescript theme={null}
const request = await client.aiGateway.logs.request(
  'my-gateway',
  logId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

### Get log response

Retrieve the response payload for a log entry.

```typescript theme={null}
const response = await client.aiGateway.logs.response(
  'my-gateway',
  logId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

### Delete logs

Delete logs for a gateway.

```typescript theme={null}
await client.aiGateway.logs.delete(
  'my-gateway',
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    before: '2024-01-01',
  }
);
```

<ParamField path="before" type="string">
  Delete logs before this date (ISO 8601)
</ParamField>

## Datasets

Manage datasets for AI model evaluation.

### Create a dataset

```typescript theme={null}
const dataset = await client.aiGateway.datasets.create(
  'my-gateway',
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    name: 'test-dataset',
    data: [
      { input: 'Hello', expected_output: 'Hi there!' },
      { input: 'Goodbye', expected_output: 'See you later!' },
    ],
  }
);
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="name" type="string" required>
  Dataset name
</ParamField>

<ParamField path="data" type="array" required>
  Array of test cases with input and expected output
</ParamField>

### List datasets

```typescript theme={null}
for await (const dataset of client.aiGateway.datasets.list(
  'my-gateway',
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
)) {
  console.log(dataset);
}
```

### Get a dataset

```typescript theme={null}
const dataset = await client.aiGateway.datasets.get(
  'my-gateway',
  datasetId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

### Update a dataset

```typescript theme={null}
const dataset = await client.aiGateway.datasets.update(
  'my-gateway',
  datasetId,
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    name: 'updated-dataset',
  }
);
```

### Delete a dataset

```typescript theme={null}
await client.aiGateway.datasets.delete(
  'my-gateway',
  datasetId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

## Evaluations

Run model evaluations using datasets.

### Create an evaluation

```typescript theme={null}
const evaluation = await client.aiGateway.evaluations.create(
  'my-gateway',
  {
    account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
    dataset_id: datasetId,
    evaluation_type: 'exact_match',
  }
);
```

<ParamField path="gateway_id" type="string" required>
  The gateway ID
</ParamField>

<ParamField path="account_id" type="string" required>
  Your Cloudflare account ID
</ParamField>

<ParamField path="dataset_id" type="string" required>
  Dataset to evaluate against
</ParamField>

<ParamField path="evaluation_type" type="string" required>
  Evaluation metric type
</ParamField>

### List evaluations

```typescript theme={null}
for await (const evaluation of client.aiGateway.evaluations.list(
  'my-gateway',
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
)) {
  console.log(evaluation);
}
```

### Get an evaluation

```typescript theme={null}
const evaluation = await client.aiGateway.evaluations.get(
  'my-gateway',
  evaluationId,
  { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' }
);
```

<ResponseField name="id" type="string">
  Evaluation ID
</ResponseField>

<ResponseField name="status" type="string">
  Evaluation status: 'pending', 'running', or 'complete'
</ResponseField>

<ResponseField name="score" type="number">
  Evaluation score (0-1)
</ResponseField>

<ResponseField name="results" type="array">
  Detailed results per test case
</ResponseField>

## Using AI Gateway

### With OpenAI SDK

```typescript theme={null}
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai',
});

const completion = await openai.chat.completions.create({
  model: 'gpt-3.5-turbo',
  messages: [{ role: 'user', content: 'Hello!' }],
});
```

### With Workers AI

```typescript theme={null}
const response = await fetch(
  'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/@cf/meta/llama-2-7b-chat-int8',
  {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${CLOUDFLARE_API_TOKEN}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      prompt: 'Tell me a joke',
    }),
  }
);
```

## Best practices

1. **Caching**: Enable caching for repeated queries to reduce costs and latency
2. **Rate limiting**: Set appropriate rate limits to control costs
3. **Logging**: Enable logging for debugging and analytics
4. **Monitoring**: Regularly review logs and analytics for usage patterns
5. **Security**: Use authentication to protect your gateway from unauthorized access
6. **Cost control**: Set up alerts for spending thresholds
