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

# Batch

> Submit, list, poll, cancel, and delete asynchronous batches of inference requests. See https://openrouter.ai/docs/batch-quickstart.

## Overview

Submit, list, poll, cancel, and delete asynchronous batches of inference requests. See [https://openrouter.ai/docs/batch-quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Available Operations

* [List](#list) - List batches
* [CreateBatches](#createbatches) - Create a batch
* [Delete](#delete) - Delete a batch
* [GetBatches](#getbatches) - Get a batch
* [Cancel](#cancel) - Cancel a batch
* [GetResults](#getresults) - Get batch results

## List

Lists batches in the workspace of the authenticating API key, newest first. To fetch the next page, pass the previous page's `last_id` as `after`. List items omit `results`. Use `GET /batches/{id}` to get them. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.List(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        for {
            // handle items

            res, err = res.Next()

            if err != nil {
                // handle error
            }

            if res == nil {
                break
            }
        }
    }
}
```

### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. |
| `request` | [operations.ListBatchesRequest](../../models/operations/listbatchesrequest.mdx) | ✔️ | The request object to use for the request. |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. |

### Response

**[\*operations.ListBatchesResponse](../../models/operations/listbatchesresponse.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchErrorResponse | 400, 401, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |

## CreateBatches

Creates a batch of requests that run asynchronously against a single endpoint (`/v1/chat/completions`, `/v1/responses`, `/v1/messages`, `/v1/embeddings`, `/v1/audio/speech`, `/v1/images/generations`). Returns `202` with `status: "validating"`. Poll `GET /batches/{id}` for progress and results. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage: chatCompletions

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"github.com/OpenRouterTeam/go-sdk/models/components"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.CreateBatches(ctx, components.BatchSubmitBody{
        Endpoint: components.EndpointRootV1ChatCompletions,
        Model: "openai/gpt-4o",
    })
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Example Usage: messages

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"github.com/OpenRouterTeam/go-sdk/models/components"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.CreateBatches(ctx, components.BatchSubmitBody{
        Endpoint: components.EndpointRootV1Messages,
        Model: "openai/gpt-5-nano",
    })
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Example Usage: providerPinned

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"github.com/OpenRouterTeam/go-sdk/models/components"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.CreateBatches(ctx, components.BatchSubmitBody{
        Endpoint: components.EndpointRootV1ChatCompletions,
        Model: "google/gemini-3.6-flash",
    })
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Example Usage: responses

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"github.com/OpenRouterTeam/go-sdk/models/components"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.CreateBatches(ctx, components.BatchSubmitBody{
        Endpoint: components.EndpointRootV1Responses,
        Model: "openai/gpt-4o",
    })
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. |
| `request` | [components.BatchSubmitBody](../../models/components/batchsubmitbody.mdx) | ✔️ | The request object to use for the request. |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. |

### Response

**[\*components.BatchObject](../../models/components/batchobject.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchErrorResponse | 400, 401, 402, 403, 404, 413, 422, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |

## Delete

Deletes a batch in a terminal status (`completed`, `failed`, `expired`, or `cancelled`) and its stored requests and results. Batches still in progress return `409`. Billing and usage records are kept. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.Delete(ctx, "batch_abc123")
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Parameters

| Parameter | Type | Required | Description | Example |
| - | - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. | |
| `id` | `string` | ✔️ | The batch job id returned from submit. | batch\_abc123 |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. | |

### Response

**[\*components.BatchDeletedObject](../../models/components/batchdeletedobject.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchErrorResponse | 401, 404, 409, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502, 503 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |

## GetBatches

Returns a batch with its status and request counts. Batches in a terminal status include `results`. Failed batches report the reason in `error.message`. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.GetBatches(ctx, "batch_abc123")
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Parameters

| Parameter | Type | Required | Description | Example |
| - | - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. | |
| `id` | `string` | ✔️ | The batch job id returned from submit. | batch\_abc123 |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. | |

### Response

**[\*components.BatchObject](../../models/components/batchobject.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchPaymentRequiredResponseError | 402 | application/json |
| sdkerrors.BatchErrorResponse | 401, 404, 410, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |

## Cancel

Asks the provider to stop a batch that is still running. Cancellation is enabled per organization; other organizations get `403`. The response is the batch in its current status: it stays `in_progress` until the provider confirms, then becomes `cancelled`, or `completed`, `failed`, or `expired` when the provider job ended first. Poll until the batch has any final status. Repeating the call is safe while the batch is still `in_progress`; once it has a final status the call returns `409`. OpenRouter returns no result rows for a cancelled batch, and the provider still charges for the requests that finished before the cancellation. When the batch wrote its output to your own storage, retrieving the batch can still return that location, and the rows the provider wrote there stay yours to read. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.Cancel(ctx, "batch_abc123")
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        // handle response
    }
}
```

### Parameters

| Parameter | Type | Required | Description | Example |
| - | - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. | |
| `id` | `string` | ✔️ | The batch job id returned from submit. | batch\_abc123 |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. | |

### Response

**[\*components.BatchObject](../../models/components/batchobject.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchErrorResponse | 400, 401, 403, 404, 409, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502, 503 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |

## GetResults

Returns a cursor-paginated page of normalized OpenRouter-format results for a finished batch. Pass the previous page's `next_cursor` as `cursor` to read the next page. Pages are stable across retries for the 30-day artifact retention window. See the [Batch API Quickstart](https://openrouter.ai/docs/batch-quickstart.md).

### Example Usage

```go theme={null}
package main

import(
	"context"
	"os"
	openrouter "github.com/OpenRouterTeam/go-sdk"
	"log"
)

func main() {
    ctx := context.Background()

    s := openrouter.New(
        openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
    )

    res, err := s.Batch.GetResults(ctx, "batch_abc123", nil, nil)
    if err != nil {
        log.Fatal(err)
    }
    if res != nil {
        for {
            // handle items

            res, err = res.Next()

            if err != nil {
                // handle error
            }

            if res == nil {
                break
            }
        }
    }
}
```

### Parameters

| Parameter | Type | Required | Description | Example |
| - | - | - | - | - |
| `ctx` | [context.Context](https://pkg.go.dev/context#Context) | ✔️ | The context to use for the request. | |
| `id` | `string` | ✔️ | The batch job id returned from submit. | batch\_abc123 |
| `limit` | `*int64` | ➖ | Maximum number of result items to return, from 1 through 1000. | 500 |
| `cursor` | `*string` | ➖ | Opaque pagination cursor from the previous page's `next_cursor`. Omit for the first page. | djE6YmF0Y2hfYWJjMTIzOjEwMjQ |
| `opts` | \[][operations.Option](../../models/operations/option.mdx) | ➖ | The options for this request. | |

### Response

**[\*operations.GetBatchResultsResponse](../../models/operations/getbatchresultsresponse.mdx), error**

### Errors

| Error Type | Status Code | Content Type |
| - | - | - |
| sdkerrors.BatchPaymentRequiredResponseError | 402 | application/json |
| sdkerrors.BatchErrorResponse | 400, 401, 404, 409, 410, 413, 429 | application/json |
| sdkerrors.BatchErrorResponse | 500, 502 | application/json |
| sdkerrors.APIError | 4XX, 5XX | \*/\* |
