API Keys Explained: How OpenClaw Connects to LLM Providers
What an API key is, why OpenClaw needs one, how token billing works, and how to get and safely store keys from Anthropic, OpenAI and Google in 2026.
OpenClaw needs an API key because it does not run a model itself: it sends every prompt to a provider such as Anthropic, OpenAI or Google, and the key tells that provider which account to authorize and bill. Get a key from the provider's console, keep it in the environment or OpenClaw's secret store rather than in openclaw.json, and set a spending limit before you leave an agent running. The rest of this page covers how billing works, what the main models cost today, and how to get each key.
#What is an API key?
An API key is a unique string that acts as a password between your software and a service provider. When your OpenClaw agent needs to process a message, make a decision or write a reply, it sends a request to an LLM provider. The key tells the provider who is making the request so it can authorize it and bill the right account.
An Anthropic key, for example, starts with sk-ant-:
sk-ant-api03-Abc123XyZ...Think of an API key like a card number for AI services. It identifies your account, authorizes your usage and decides where the bill goes. Treat it the same way: never share it publicly.
#Why does OpenClaw need one?
OpenClaw is an agent platform, not an AI model. It orchestrates everything around the model: messaging channels, skills, memory and the agent lifecycle. The intelligence comes from a provider's model, which runs on that provider's infrastructure. Every time your agent handles a message:
- Your agent receives a message through a connected channel, such as Telegram or Discord.
- OpenClaw builds a prompt from the message, the conversation history, your instructions and the available tools.
- OpenClaw sends that prompt to the provider's API, authenticated with your key.
- The provider runs the model and returns a response.
- OpenClaw parses the response, runs any requested tool calls and sends the reply back through the channel.
Step 3 is where the key matters. Without a valid one, the provider rejects the request and your agent cannot respond.
Want step-by-step guides for this and more?
ClawDocx Pro includes 500+ curated prompts, setup guides, SKILL.md files, and templates — everything to make your AI agent unstoppable.
See plans & pricing#How does LLM billing work?
Providers charge per token, a fragment of text the model reads or writes. Both the text you send (input tokens) and the text the model generates (output tokens) count, and output is priced higher. How many tokens a given text becomes depends on the model's tokenizer: Anthropic notes that Claude 4.7 and later models produce "approximately 30% more tokens for the same text" than earlier Claude models.
A simplified example of one turn:
- Your message to the agent: about 50 tokens
- Conversation history and system prompt that OpenClaw adds: about 2,000 tokens
- The model's reply: about 300 tokens
At Claude Sonnet 5.5's rates of $2 per million input tokens and $10 per million output tokens, those 2,050 input and 300 output tokens cost about $0.007. A hundred turns a day at that size comes to roughly $21 a month.
#What do the main models cost?
Rates below are per million tokens, current as of November 14, 2026, from each provider's own pricing page.
| Provider | Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|---|
| Anthropic | Claude Opus 5.5 | $4.00 | $20.00 |
| Anthropic | Claude Sonnet 5.5 | $2.00 | $10.00 |
| Anthropic | Claude Haiku 5.5 | $0.10 up to 100K tokens / $0.50 above | $0.50 up to 100K tokens / $2.50 above |
| OpenAI | GPT-6 Astra | $10.00 | $50.00 |
| OpenAI | GPT-6.1 Sol | $2.00 | $10.00 |
| OpenAI | GPT-6 Luna | $0.10 | $0.50 |
| Gemini 4 Hydrogen | $1.50 | $7.50 | |
| Gemini 3.8 Flash | $0.75 | $3.75 |
Several of those rates move with context length or with a date. OpenAI's are short-context prices: OpenClaw's OpenAI models page puts it plainly for Astra, "Requests above 272K input tokens have higher rates", and above that threshold GPT-6 Astra costs $20 and $75, GPT-6.1 Sol $4 and $15, and GPT-6 Luna $0.20 and $0.75. Gemini 3.8 Flash is $0.75 and $3.75 through December 31, 2026 and $1.50 and $7.50 from January 1, 2027. For the full comparison, including Fable 5.2, Gemini 4 Carbon and open-weight models, see the best LLM models for OpenClaw.
Output tokens cost more than input tokens on every model above, often five times as much. Asking your agent for concise replies in its SOUL.md personality file is one of the cheapest savings available.
#How do you get each provider's key?
#Anthropic (Claude)
- Go to the Claude Console at platform.claude.com and sign in, or create an account.
- Open the API keys page at
platform.claude.com/settings/keys. - Click Create key, name it (for example
openclaw-agent), choose an expiration, and set the linked account to yourself or a service account. - Copy the key straight away. The Console shows the full key only once, at creation.
Anthropic recommends a personal key for your own development and a service account key "for anything shared", such as an agent several people rely on.
OpenClaw reads an optional JSON5 config from ~/.openclaw/openclaw.json. The key belongs in the environment rather than in that file, which the next section covers; the config only names the model, as a provider/model ref:
// ~/.openclaw/openclaw.json{ agents: { defaults: { model: { primary: "anthropic/claude-sonnet-5-5" }, }, },}#OpenAI (GPT)
- Sign in at platform.openai.com.
- Open the API keys page and create a key.
- Copy it and store it safely before you leave the page.
OpenClaw's OpenAI models page points a new setup at GPT-6 Astra: "Fresh API-key setup uses Astra."
// ~/.openclaw/openclaw.json{ agents: { defaults: { model: { primary: "openai/gpt-6-astra" }, }, },}#Google (Gemini)
- Sign in to Google AI Studio and accept the terms. If you are a new user, AI Studio creates a default Google Cloud project and API key for you.
- To make another key, open the API Keys page and click Create API key.
- Copy the key.
// ~/.openclaw/openclaw.json{ agents: { defaults: { model: { primary: "google/gemini-4-hydrogen" }, }, },}Google AI Studio's free tier covers some Gemini models, including Gemini 4 Hydrogen and Gemini 3.8 Flash, which makes it a cheap way to try OpenClaw before committing to a paid provider. Gemini 4 Carbon and Gemini 4 Argon are not on the free tier.
#How do you keep your keys safe?
A leaked key lets someone else run up charges on your account or use it for something worse. Follow these rules.
Never commit keys to Git. Keep openclaw.json, ~/.openclaw/.env and anything else that holds a credential out of the repository. Better still, keep the key out of the config file altogether and let OpenClaw read it from the environment.
# Use environment variables instead of hardcoding keysexport ANTHROPIC_API_KEY="sk-ant-your-key-here"When a provider entry does need a key, prefer a SecretRef over a literal. A plaintext string still works, because "Secret refs are additive: plaintext values still work", but a ref is the safer form: the config names where the secret lives instead of holding it. The documented shape is { source: "env" | "file" | "exec" | "store", provider: "default", id: "..." }:
// ~/.openclaw/openclaw.json{ models: { providers: { anthropic: { apiKey: { source: "env", provider: "default", id: "ANTHROPIC_API_KEY" }, }, }, },}Across more than one machine, put the value in OpenClaw's shared secret store and point the same field at it with source: "store". New names ending in a credential suffix such as _API_KEY are stored as secrets, and the CLI accepts --value "only when the resolved kind is env", so it takes a secret from piped stdin, from --value-file <path>, or from a prompt:
openclaw secrets store set ANTHROPIC_API_KEYSet a spending limit on every provider account that offers one. It is your safety net: if a key leaks, the limit caps the damage.
Rotate keys periodically. Create a new key every few months and delete the old one, which limits how long a leaked key stays useful. Anthropic's keys can also carry an expiration date from the start.
Use separate keys for separate purposes. If you run several agents or other applications, give each its own key. You can then track usage per application and revoke one without breaking the others.
#How do you use more than one provider?
OpenClaw can use several providers at once. A common setup runs Claude Sonnet 5.5 as the primary model with GPT-6 Astra as the failover behind it. Each provider still needs its own key in the environment; the config file only names the refs.
// ~/.openclaw/openclaw.json{ agents: { defaults: { model: { primary: "anthropic/claude-sonnet-5-5", fallbacks: ["openai/gpt-6-astra"], }, }, },}That configuration gives your agent resilience. OpenClaw's model failover documentation describes two stages: first "Auth-profile rotation within the current provider", then "Model fallback to the next model in agents.defaults.model.fallbacks". The list is failover, not per-task routing: nothing in it sends code generation to one model and quick replies to another.
To keep routine work on a cheaper model, use the levers OpenClaw provides for it. Recurring checks are now ordinary automation jobs: the old agents.defaults.heartbeat block "has moved to ordinary automation jobs", and openclaw doctor --fix migrates it, preserving the job's "schedule, scratch, model policy, and delivery settings". For per-agent models and the utility model that handles OpenClaw's own housekeeping calls, see the routing section of the best LLM models for OpenClaw.
#What's next?
You now know what an API key is, how billing works and how to set up each provider account.
- Choosing a model? The Best LLM Models for OpenClaw in 2026 compares every major model with current prices.
- Setting up OpenClaw? Getting Started with OpenClaw walks through the installation.
- Storing keys properly? API key setup covers environment variables, SecretRefs and the secret store in more depth.
- Watching the bill? The cost optimization guide covers budgeting and the habits that keep spend down.
Frequently asked questions
- Why does OpenClaw need an API key?
- OpenClaw is an agent platform, not a model. It sends each prompt to a model provider such as Anthropic, OpenAI or Google, and the API key tells that provider which account is making the request and should be billed. Without a valid key, the provider rejects the request and the agent cannot respond.
- How much does an OpenClaw agent cost to run on an API key?
- It depends on the model and how much text each turn carries. A turn with about 2,050 input tokens and 300 output tokens costs about $0.007 on Claude Sonnet 5.5 at $2 and $10 per million tokens, so a hundred such turns a day comes to roughly $21 a month. Cheaper models such as Claude Haiku 5.5 or GPT-6 Luna cut that by an order of magnitude.
- Where should I put my API key for OpenClaw?
- In the environment, not in openclaw.json. Export it as a variable such as ANTHROPIC_API_KEY, or put it in OpenClaw's secret store with openclaw secrets store set, and point any provider entry that needs it at a SecretRef rather than a literal string.
- Which provider has a free tier for OpenClaw?
- Google AI Studio. Its free tier covers models including Gemini 4 Hydrogen and Gemini 3.8 Flash, and new users get a default Google Cloud project and API key automatically after accepting the terms.
- Can OpenClaw use more than one provider at once?
- Yes. Name a primary model and an ordered list of fallbacks under agents.defaults.model, with each provider's key in the environment. OpenClaw first rotates auth profiles within the current provider, then moves to the next fallback model.
Sources
- OpenClaw: Configuration
- OpenClaw: Models CLI
- OpenClaw: Model failover
- OpenClaw: OpenAI models
- OpenClaw: Google (Gemini)
- OpenClaw: Configuration: environment, secrets, and includes
- OpenClaw: Secrets CLI
- OpenClaw: Configuration: agent heartbeat, compaction, and streaming
- Claude: Get your API key
- OpenAI API quickstart
- Google AI Studio: Using Gemini API keys
- Claude API pricing
- OpenAI API pricing
- Gemini Developer API pricing