# AI agents (/docs/agents) Let a coding agent set up hackrouter, with docs and a skill written for agents. Coding agents can read these docs, and set hackrouter up for you. ## Ask your agent [#ask-your-agent] Paste this into Claude Code, Cursor, Codex or any coding agent: > Integrate hackrouter into this project. Read https://hackrouter.ai/llms-full.txt first. Use the base URL https://api.hackrouter.ai/v1 for OpenAI clients or https://api.hackrouter.ai for Anthropic clients, read the API key from the HACKROUTER_KEY environment variable, and pick a model from https://api.hackrouter.ai/v1/models. Keep the key itself out of the conversation: set `HACKROUTER_KEY` in your environment, and let the code read it. ## Install the skill [#install-the-skill] The hackrouter skill teaches an agent the endpoints, how to pick a model, what each error means and how to look up costs. For Claude Code: ```bash mkdir -p ~/.claude/skills/hackrouter curl -fsSL https://hackrouter.ai/skills/hackrouter/SKILL.md \ -o ~/.claude/skills/hackrouter/SKILL.md ``` Claude Code uses it whenever a task involves hackrouter. For a single project, put it in `.claude/skills/hackrouter/` in the repository instead. Other agents can read the same file: [SKILL.md](https://hackrouter.ai/skills/hackrouter/SKILL.md). ## Docs as Markdown [#docs-as-markdown] | File | What it holds | |---|---| | [`/llms.txt`](https://hackrouter.ai/llms.txt) | The index of the docs, a line per page | | [`/llms-full.txt`](https://hackrouter.ai/llms-full.txt) | Every page in one file, with current prices and limits | | `/docs/.md` | One page: add `.md` to any docs URL, such as [`/docs/errors.md`](https://hackrouter.ai/docs/errors.md) | The model list is also available to agents directly, without a key: `GET https://api.hackrouter.ai/v1/models`. # API reference (/docs/api) Every endpoint, what it accepts beyond its upstream API, and what it returns. Base URL `https://api.hackrouter.ai/v1`. Send your key as `Authorization: Bearer hr-...` or `x-api-key: hr-...`. Amounts are exact decimal strings in US dollars; prices are per million units. | Endpoint | | Key | |---|---|---| | [`POST /chat/completions`](#chat-completions) | OpenAI Chat Completions | Yes | | [`POST /responses`](#responses) | OpenAI Responses | Yes | | [`POST /messages`](#messages) | Anthropic Messages | Yes | | [`POST /images/generations`](#images) | OpenAI Images | Yes | | [`GET /models`](#models) | Models, APIs and prices | No | | [`GET /account`](#account) | Balance and the key's limit | Yes | | [`GET /requests/{id}`](#requests) | What one request used and cost | Yes | | [`POST /estimates`](#estimates) | The charge for a given usage | Yes | | [`POST /files`](#files) | An upload link for large media | Yes | The four model APIs behave as their originals do. Fields not listed below are passed to the model unchanged. Request bodies can be up to 20 MiB. ## Chat completions [#chat-completions] `POST /v1/chat/completions` takes OpenAI's Chat Completions request. hackrouter checks: - `model` is required, and must offer `openai_chat`. - `max_tokens` and `max_completion_tokens` are positive integers; if both are set, `max_completion_tokens` wins. The output is capped at the model's maximum. - `n` is 1 to 128. - `service_tier` is `auto` or `default`. - `web_search_options` and non-text `modalities` are refused: they would be billed by the provider in ways hackrouter does not meter. ## Responses [#responses] `POST /v1/responses` takes OpenAI's Responses request, and is stateless: nothing is stored. - `previous_response_id`, `conversation` and `prompt` must be left unset, and `background` false. - `max_output_tokens` is a positive integer. - `service_tier` is `auto` or `default`. - Tools are `function` or `custom`. Built-in tools are refused. ## Messages [#messages] `POST /v1/messages` takes Anthropic's Messages request. The `anthropic-version` header is passed on, `2023-06-01` if you leave it out. - `max_tokens` is required. - Tools run on your side: custom tools and Anthropic's client tools (bash, text editor, computer, memory). Server tools such as web search are refused. - `service_tier` is `auto` or `standard_only`. - Beta features that Anthropic charges extra for are removed from `anthropic-beta`. ## Images [#images] `POST /v1/images/generations` takes OpenAI's image request. - `model` and `prompt` are required; `n` is 1 to 10. - For models priced per image, `size` and `quality` must be `auto` or one the model offers. - With `response_format: "url"`, images come back as links valid for 7 days. Otherwise they come back inline as `b64_json`. An image is charged once it has started, even if you disconnect. ## Models [#models] `GET /v1/models` and `GET /v1/models/{id}` need no key. ```json { "object": "list", "data": [ { "id": "deepseek-v4-pro", "object": "model", "owned_by": "hackrouter", "context_length": 128000, "max_output_tokens": 8192, "endpoints": ["openai_chat", "anthropic_messages"], "prices": { "input_tokens": "1.32", "output_tokens": "3.96" } } ] } ``` `endpoints` lists the APIs the model can be called through. `prices` gives each meter in dollars per million units; see [what is metered](/docs/models#what-is-metered). ## Account [#account] `GET /v1/account` returns the balance, and the monthly limit of the key you called with. ```json { "object": "account", "id": "…", "balance": "12.48", "api_key": { "id": "…", "monthly_limit": null } } ``` ## Requests [#requests] `GET /v1/requests/{id}` returns what a charged request used and cost. The ID is the `x-request-id` of its response. Charges are recorded within a minute or two; until then, and for failed requests, it returns `404 request_not_found`. ```json { "object": "request", "id": "0193…", "model": "deepseek-v4-pro", "usage": { "input_tokens": 1820, "output_tokens": 412 }, "is_estimated": false, "charge": "0.004034", "started_at": "…", "duration_ms": 3120, "first_byte_ms": 410 } ``` `is_estimated` is true when the provider did not report usage and it was estimated from the text. ## Estimates [#estimates] `POST /v1/estimates` prices a usage without sending anything to a model. ```bash curl https://api.hackrouter.ai/v1/estimates \ -H "Authorization: Bearer $HACKROUTER_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-v4-pro", "usage": { "input_tokens": 2000, "output_tokens": 500 } }' ``` `usage` takes the [meters](/docs/models#what-is-metered) as whole numbers; missing ones count as 0. The answer gives the total `charge` and the charge per meter. ## Files [#files] `POST /v1/files` gives you a link to upload large media to, and a link to put in your request instead of the bytes. ```json { "content_type": "image/png", "bytes": 2048000 } ``` - `content_type` is an image, video or audio type, or `application/pdf`. `bytes` is up to 200 MiB. - The answer has an `upload` to `PUT` the file to, valid for 15 minutes, and a `url` valid for 7 days. - Creating a file is free, but needs a balance above zero. ## Response headers [#response-headers] | Header | Meaning | |---|---| | `x-request-id` | The request's ID, for [`GET /requests/{id}`](#requests) and support | | `x-ratelimit-limit-requests` | Requests per minute | | `x-ratelimit-remaining-requests` | How many can be sent right now | | `x-ratelimit-reset-requests` | When the full burst is available again | | `retry-after` | On `429` only: seconds to wait | Errors and their codes are in [Errors](/docs/errors). # Authentication (/docs/authentication) Keys, where to send them, and what happens when one is revoked. Every request except [`GET /v1/models`](/docs/api#models) needs a key. Create keys in the [console](/console/keys); each starts with `hr-`. Send it either way: ```http Authorization: Bearer hr-... x-api-key: hr-... ``` If both are present, `Authorization` is used. ## Keys [#keys] - **One balance.** All keys of an account draw on the same balance. Use one per project or environment to see what each spends. - **Monthly limits.** A key can have a limit in dollars per calendar month, in UTC. Past it, that key gets `402 key_limit_reached` and the other keys carry on. - **Revoking.** A revoked key stops working within a minute, everywhere. A missing, wrong or revoked key gets `401 invalid_api_key`. See [Errors](/docs/errors). # Billing (/docs/billing) A prepaid balance, drawn per request at each model's price. You top up a balance in advance, and each request is paid from it. There is no plan, no seat and nothing that renews. ## How a request is charged [#how-a-request-is-charged] - **By usage.** Each meter used times its price, added up and rounded up to a billionth of a dollar. See [Models and prices](/docs/models). - **At the price of the moment.** Each request keeps the prices it was charged at. - **Failures are free.** A request that is refused, rejected by the model or fails before answering costs nothing. - **What was produced is paid.** If a stream breaks or you disconnect partway, you pay for what was generated. An image is charged whole once it has started. - **Estimated when it must be.** When a provider does not report usage, it is estimated from the text, and the request is marked as estimated. Look up what any request cost with [`GET /v1/requests/{id}`](/docs/api#requests), or work out a charge before sending with [`POST /v1/estimates`](/docs/api#estimates). ## The balance [#the-balance] - **Holds.** Before a request runs, the most it could cost is set aside and released when it ends. With a low balance, the output is limited so the request cannot cost more than what is left. - **Empty.** When the balance cannot cover a request, it gets `402 insufficient_balance`. Nothing runs. - **Low-balance email.** Set a threshold in the console and get one email when the balance falls below it. ## Topping up [#topping-up] Top up on **Billing** in the [console](/console/billing), by card. Larger total top-ups also raise your [rate limit](/docs/rate-limits). # Claude Code (/docs/claude-code) Run Claude Code on any model through hackrouter. Claude Code talks to the Anthropic API. Point it at hackrouter with three variables, then start it as usual. ```bash export ANTHROPIC_BASE_URL=https://api.hackrouter.ai export ANTHROPIC_AUTH_TOKEN=$HACKROUTER_KEY export ANTHROPIC_MODEL=deepseek-v4-pro claude ``` `ANTHROPIC_MODEL` takes any model ID from [Models](/docs/models). Models that are not Anthropic's own are translated from the Messages API, so Claude Code works with them too. To keep the settings for good, put the same variables in the `env` block of `~/.claude/settings.json`. ## What does not carry over [#what-does-not-carry-over] - **Server tools.** Web search, web fetch and code execution run on Anthropic's side and are billed there. hackrouter refuses requests that ask for them, so Claude Code's built-in web search does not work here. Tools that run on your machine, such as editing files and running commands, work as usual. - **Priced betas.** Beta features that Anthropic charges extra for, such as the 1M-token context, are removed from the request. Each turn shows up in the console under **Requests**, with the model, tokens and charge. # Codex (/docs/codex) Run OpenAI's Codex CLI through hackrouter. Add hackrouter as a model provider in `~/.codex/config.toml`: ```toml model = "deepseek-v4-pro" model_provider = "hackrouter" [model_providers.hackrouter] name = "hackrouter" base_url = "https://api.hackrouter.ai/v1" env_key = "HACKROUTER_KEY" wire_api = "responses" ``` Then export your key and start Codex: ```bash export HACKROUTER_KEY=hr-... codex ``` `model` takes any model ID whose `endpoints` include `openai_responses` on [`GET /v1/models`](/docs/api#models). hackrouter does not store responses. Fields that point at a stored one, `previous_response_id`, `conversation` and `background`, are refused. # Errors (/docs/errors) One error shape, a code to act on, and whether to retry. Errors come back with a code your program can act on, and the request ID to quote when asking about it: ```json { "error": { "type": "billing_error", "code": "insufficient_balance", "message": "…", "param": null }, "request_id": "0193…" } ``` On `/v1/messages`, the body has Anthropic's shape: `"type": "error"` at the top and no `param`. Act on `code`, not on `message`. | Code | What to do | |---|---| | `invalid_request`, `body_not_json` 400 | Fix the request. The message says what is wrong. | | `rejected_by_model` 400 / 422 | The model refused the request, or it asks for a feature that cannot be translated. Change the request or the model. | | `invalid_api_key` 401 | Check the key; it may have been revoked. | | `insufficient_balance` 402 | Top up. | | `key_limit_reached` 402 | Raise the key's monthly limit, or wait for next month (UTC). | | `account_suspended` 403 | Write to [contact@hackrouter.ai](mailto:contact@hackrouter.ai). | | `country_not_supported` 403 | hackrouter is not available in mainland China. | | `model_not_found` 404 | Use an ID from [Models](/docs/models) that offers the API you called. | | `request_not_found` 404 | The request has not been charged yet, or is not yours. Try again in a minute or two. | | `route_not_found` 404 | Check the path. | | `method_not_allowed` 405 | Use a method from the `allow` header. | | `body_too_large` 413 | The body is over 20 MiB. Send media by URL; see [Files](/docs/api#files). | | `rate_limited` 429 | Wait for `retry-after` seconds. See [Rate limits](/docs/rate-limits). | | `internal_error` 500 | Retry. | | `model_unavailable` 503 | Every route for the model failed. Retry, or try another model. | | `storage_unavailable` 503 | Retry later. | None of these are charged. A stream that breaks after it started ends with an error event whose code is `stream_broken`; what was sent is charged. See [Streaming](/docs/streaming). Every response carries an `x-request-id` header with the same ID. # Overview (/docs) One key and one balance for many models, through the OpenAI and Anthropic APIs you already use. hackrouter is an API for language and image models. You call it the way you call OpenAI or Anthropic, with a different base URL and a hackrouter key, and every request is paid from your prepaid balance. | | Base URL | |---|---| | OpenAI clients | `https://api.hackrouter.ai/v1` | | Anthropic clients | `https://api.hackrouter.ai` | Anthropic's SDKs add `/v1` themselves, so their base URL stops at the host. ## What you get [#what-you-get] - **The API you already use.** Chat Completions, Responses, Messages and Images, passed through in their own format. Streaming, tool calls and images work unchanged. - **Any model through either format.** A model that does not speak your client's format natively is translated between OpenAI Chat and Anthropic Messages. - **Requests that find a way.** If a provider fails before it answers, the request moves to another route. You only see the answer. - **A bill you can check.** Each request records its usage and the prices of that moment. Failed requests cost nothing. ## Start here [#start-here] - [Quickstart](/docs/quickstart): A key, a base URL and your first request, in a few minutes. - [Models and prices](/docs/models): Every model on offer, priced per million tokens. - [Claude Code](/docs/claude-code): Point Claude Code at hackrouter with three variables. - [API reference](/docs/api): Endpoints, parameters, errors and headers. # Models and prices (/docs/models) Every model on offer, called by its ID, priced per million tokens. Call a model by its ID, the same through every API. Prices are in US dollars per million tokens and the same for every account. | Model | Context | Input | Cache read | Output | |---|---|---|---|---| | `deepseek-v4.1-flash` | 1M | $0.24 | $0.03 | $1.05 | | `deepseek-v4.1-flash-uncensored` | 1M | $0.24 | $0.03 | $1.05 | | `glm-5.3-flash` | 200K | $0.24 | $0.09 | $1.26 | | `kimi-k3.1-pro` | 1M | $5.40 | $0.60 | $12.00 | The list is also available without a key, with every price and the APIs each model can be called through: ```bash curl https://api.hackrouter.ai/v1/models ``` ## What is metered [#what-is-metered] A request is charged for what it used, each kind of token at its own price. A token counts once: a cached input token is charged as a cache read, not also as input. | Meter | What it counts | |---|---| | `input_tokens` | Input, other than cache reads and writes | | `cache_read_tokens` | Input read from the prompt cache | | `cache_write_5m_tokens` | Input written to the cache for five minutes | | `cache_write_1h_tokens` | Input written to the cache for an hour | | `output_tokens` | Output, reasoning included | | `output_images` | Images generated | Response bodies show your model ID, never the provider's. # Privacy (/docs/privacy) What is kept about each request, and what never is. hackrouter does not store prompts or answers. They pass through to the model and back. For each request, it keeps what billing and debugging need: - **Charged requests:** the request ID, the key, the model, the token counts, the prices and the charge, and timings. - **Failed requests:** the request ID, the key, the model as you wrote it, the reason and the status. Logs hold the same kind of facts, never the content of a request or an answer. The Responses API is stateless here: nothing is stored to resume later. Files you upload, and images returned as links, are kept for 7 days. hackrouter is not available in mainland China. Questions about your data: [contact@hackrouter.ai](mailto:contact@hackrouter.ai). # Quickstart (/docs/quickstart) From no key to your first answer. ### Create a key [#create-a-key] Sign in to the [console](/console) and create a key on **API keys**. It is shown once: keep it in an environment variable. ```bash export HACKROUTER_KEY=hr-... ``` New accounts get free credit to start, once per email address. ### Send a request [#send-a-request] Use any model ID from [Models](/docs/models). The examples call `deepseek-v4-pro`. **curl** ```bash curl https://api.hackrouter.ai/v1/chat/completions \ -H "Authorization: Bearer $HACKROUTER_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-v4-pro", "messages": [{ "role": "user", "content": "Hello" }] }' ``` **Python** ```python import os from openai import OpenAI client = OpenAI( base_url="https://api.hackrouter.ai/v1", api_key=os.environ["HACKROUTER_KEY"], ) answer = client.chat.completions.create( model="deepseek-v4-pro", messages=[{"role": "user", "content": "Hello"}], ) print(answer.choices[0].message.content) ``` **TypeScript** ```ts import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://api.hackrouter.ai/v1", apiKey: process.env.HACKROUTER_KEY, }); const answer = await client.chat.completions.create({ model: "deepseek-v4-pro", messages: [{ role: "user", content: "Hello" }], }); console.log(answer.choices[0].message.content); ``` **Anthropic SDK** ```python import os from anthropic import Anthropic client = Anthropic( base_url="https://api.hackrouter.ai", api_key=os.environ["HACKROUTER_KEY"], ) message = client.messages.create( model="deepseek-v4-pro", max_tokens=1024, messages=[{"role": "user", "content": "Hello"}], ) print(message.content[0].text) ``` ### See it in the console [#see-it-in-the-console] The request shows up under **Requests** with its tokens, timing and charge. Charges reach your balance within a minute or two. # Rate limits (/docs/rate-limits) Requests per minute, by account, rising with what you have topped up. Limits apply per account, across all its keys. The tier depends on how much you have topped up in total, free credit included: | Topped up in total | Requests a minute | Burst | |---|---|---| | From $0.00 | 60 | 20 | | From $5.00 | 300 | 100 | | From $50.00 | 1,000 | 300 | | From $500.00 | 5,000 | 1,000 | Requests are spaced evenly over the minute, and up to the burst can arrive at once. Every request counts, including those refused for the balance. ## Headers [#headers] | Header | Meaning | |---|---| | `x-ratelimit-limit-requests` | Requests per minute | | `x-ratelimit-remaining-requests` | How many can be sent right now | | `x-ratelimit-reset-requests` | When the full burst is available again, such as `12s` | | `retry-after` | On `429` only: seconds to wait | Past the limit, a request gets `429 rate_limited`. Wait for `retry-after` and send it again. Need a higher limit? Write to [contact@hackrouter.ai](mailto:contact@hackrouter.ai) with your organization ID, from **Settings** in the console. # SDKs and tools (/docs/sdks) Anything that lets you set an OpenAI or Anthropic base URL works. hackrouter speaks the OpenAI and Anthropic APIs, so you keep your SDK and change two settings: the base URL and the key. | Client | Base URL | Key | |---|---|---| | OpenAI SDKs, LangChain, LlamaIndex, Vercel AI SDK | `https://api.hackrouter.ai/v1` | `HACKROUTER_KEY` as the API key | | Anthropic SDKs | `https://api.hackrouter.ai` | `HACKROUTER_KEY` as the API key | | Editors and agents with a custom OpenAI endpoint (Cursor, Cline, Zed and others) | `https://api.hackrouter.ai/v1` | `HACKROUTER_KEY` | The key can travel as `Authorization: Bearer hr-...` or as `x-api-key: hr-...`. Both are accepted on every endpoint. ## Which API to call [#which-api-to-call] Every model lists the APIs it can be called through in `endpoints` on [`GET /v1/models`](/docs/api#models). Chat Completions and Messages are interchangeable: when a model does not speak your client's format, the request and the answer are translated. A few features cannot be translated and are refused before anything is charged: `n` above 1, `logit_bias` and `logprobs` from Chat to Messages, and structured outputs or built-in tools from Messages to Chat. Use the model's own format for those. ## Tools with their own guide [#tools-with-their-own-guide] - [Claude Code](/docs/claude-code) - [Codex](/docs/codex) # Streaming (/docs/streaming) Server-sent events in each API's own format, and what a broken stream costs. Set `stream: true` as you would with OpenAI or Anthropic. Events arrive in the format of the API you called, with your model ID. - **Keepalive.** A comment line, `: keepalive`, is sent every 15 seconds while the model is quiet, so proxies do not close the connection. - **Usage in the stream.** On Chat Completions, set `stream_options.include_usage` to get the usage in a final chunk. The request is charged on that usage either way. - **A broken stream.** If the provider fails after the answer has started, the stream ends with an error event whose code is `stream_broken`. It is not retried. You pay for what was sent. - **Disconnecting.** If you close the connection partway, the answer stops and you pay for what was generated up to then. Before the first byte, a failing provider is replaced by another route, so a stream that has started comes from one model and one answer.