Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Setup guides

Setup guide · Models

Get an OpenRouter key

A personal OpenRouter account with credits, a key with a credit limit kept in a project .env, and exercise code that runs on any model through the Anthropic or OpenAI SDK.

15 min Updated 2026-10-31

This is optional. Most exercises run on your Claude subscription through Claude Code or claude -p, and those never need a key. Exercises tagged API run code that calls a model directly (the Claude Agent SDK, the Anthropic or OpenAI SDK, or embeddings). OpenRouter is one way to run them: one key and one prepaid balance give you hundreds of models from Anthropic, OpenAI, Google, DeepSeek, Qwen and other open-weight labs, behind both an Anthropic-compatible and an OpenAI-compatible API. The other way is a free local model; see OpenRouter or a local model? to choose.

Before you start

  • A personal email address or login for a new OpenRouter account.
  • A payment method. OpenRouter accepts major credit cards, AliPay and USDC.
  • Small budgets go a long way. Per million input tokens, anthropic/claude-haiku-5.5 costs $0.10, anthropic/claude-sonnet-5.5 $2 and deepseek/deepseek-v4-flash about $0.09. A typical exercise costs a few cents.

1. Create an account and buy credits

  1. Sign up at openrouter.ai.
  2. Open the Credits page and buy a small amount, such as $10.

Credits are a prepaid balance in US dollars, and each request's cost is deducted from it. OpenRouter passes model prices through at the provider's list price and charges a fee when you buy credits instead. Unused credits can be refunded only within 24 hours of purchase, and OpenRouter may expire them a year after purchase, so buy small amounts. Buying at least 10 credits also raises your free-model limit (step 5).

2. Create a key with a credit limit

  1. Open the API keys page and create a key.
  2. Name it (for example learning-laptop) and set a credit limit, such as $5. You can also make the limit reset daily, weekly or monthly.
  3. Copy the key, which starts with sk-or-. If you lose it, delete it and create a new one.

Once a key has used its limit, requests with it are rejected until you raise the limit or it resets. A limit per key means a runaway agent loop costs you at most that amount. Check a key's remaining limit at any time:

curl -s https://openrouter.ai/api/v1/key -H "Authorization: Bearer $OPENROUTER_API_KEY"

3. Keep the key in the project's .env

Exercise answers read their configuration from a .env file in the project folder and load it only for the scripts that need it (node --env-file-if-exists=.env, uv run --env-file .env). Make sure git ignores it:

echo ".env" >> .gitignore
git check-ignore .env   # prints ".env" when it's ignored

Each answer ships a .env.example; copy it to .env and paste your key into the block you use (step 6 shows both). For a quick test in one terminal you can also run export OPENROUTER_API_KEY="sk-or-...", which the curl commands on this page use.

Never in your shell profile, never in git

Don't put these variables in ~/.zshrc, ~/.bashrc or your PowerShell profile, even though OpenRouter's own Claude Code guide suggests it. Claude Code reads ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN too, so every claude -p call in your exercises would route to OpenRouter and spend your credits instead of using your subscription.

Never paste a key into a prompt, a commit, a screenshot or a shared notebook. OpenRouter is a GitHub secret-scanning partner and emails you if it finds a key exposed; delete it and create another.

4. Check the privacy settings

OpenRouter forwards each request to a provider (Anthropic, OpenAI, Google, or a host running an open-weight model). Two layers matter:

  • OpenRouter itself doesn't store prompts or responses unless you opt in. Both opt-ins are off by default: Input & Output Logging in Observability settings (saves prompts so you can read them on your Logs page), and OpenRouter Use of Inputs/Outputs in Privacy settings (lets OpenRouter use your data for a 1% discount). It always stores metadata such as token counts and latency.
  • Providers have their own policies. In your privacy settings you can stop routing to providers that may train on your data, and enforce Zero Data Retention (ZDR), which routes only to endpoints that don't store prompts. ZDR has a separate toggle per model group (Anthropic, OpenAI, Google, SpaceXAI, all other models); a request can also ask for it with "provider": {"zdr": true}.

Exercises use synthetic data only, so the defaults are fine. Turning on ZDR for all other models is a sensible extra step, and may make some models unavailable.

5. Know the free models and their limits

Some models have a free variant: append :free to a slug that lists one, such as google/gemma-4-31b-it:free. The model list filtered to free shows what's available today. They are rate-limited:

Credits bought (all time)Requests per minuteRequests per day
Fewer than 102050
At least 10201,000

Free models are fine for quick experiments but too limited for an agent loop that makes dozens of calls. A negative balance blocks free models too, with HTTP 402.

6. Pick a model and connect your code

A model is named by a slug, author/model. Ones the answers use or suggest:

SlugUse it for
anthropic/claude-sonnet-5.5Default for agent exercises: the most reliable tool use
anthropic/claude-haiku-5.5Cheap and fast; good while building and debugging
openai/gpt-6.1-solComparing against another frontier model
google/gemini-3.8-flashA fast, cheap alternative from another lab
deepseek/deepseek-v4-flash, qwen/qwen3.8-flashVery cheap open-weight models

Aliases such as ~anthropic/claude-sonnet-latest always resolve to the newest model in a family. They're handy for trying things, but use a fixed slug for a run whose numbers go in a deliverable, so you can say which model produced them. The full catalogue is at openrouter.ai/models.

Exercise code connects in one of two ways, depending on the SDK.

Claude Agent SDK and Anthropic SDK code

OpenRouter serves the Anthropic Messages API at https://openrouter.ai/api. The SDKs read these variables themselves:

.env
ANTHROPIC_BASE_URL=https://openrouter.ai/api
ANTHROPIC_AUTH_TOKEN=sk-or-...
ANTHROPIC_API_KEY=
MODEL=anthropic/claude-sonnet-5.5

ANTHROPIC_AUTH_TOKEN is sent as a bearer token, which is how OpenRouter authenticates. ANTHROPIC_API_KEY must be explicitly empty, so nothing falls back to authenticating with Anthropic directly. MODEL can be any OpenRouter slug.

Anthropic models work best here

OpenRouter only guarantees Claude Code and Agent SDK compatibility with the Anthropic first-party provider. Other models can work through this endpoint, but tool use is most reliable on anthropic/… models.

Everything else: the OpenAI SDK

Plain model calls, structured output and embeddings use the OpenAI SDK, which reads its own two variables:

.env
OPENAI_BASE_URL=https://openrouter.ai/api/v1
OPENAI_API_KEY=sk-or-...
MODEL=anthropic/claude-haiku-5.5
EMBEDDINGS_MODEL=openai/text-embedding-3-small

OpenRouter has an embeddings endpoint (/api/v1/embeddings) with models from OpenAI, Google, Qwen, Voyage and others. Both clients are created with no arguments; OpenRouter or a local model? shows the code.

Check it works

curl -s https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "content-type: application/json" \
  -d '{"model":"anthropic/claude-haiku-5.5","max_tokens":20,"messages":[{"role":"user","content":"Reply with the single word: ready"}]}' \
  | jq -r '.choices[0].message.content'

Expected output:

ready

The request also appears on your Activity page with its cost.

Troubleshooting

HTTP 401. The key is missing, mistyped or deleted. Run echo "$OPENROUTER_API_KEY" | cut -c1-8 and check that it prints sk-or-. In a project, check that you passed --env-file and that the key is in ANTHROPIC_AUTH_TOKEN (Agent SDK) or OPENAI_API_KEY (OpenAI SDK), not only in OPENROUTER_API_KEY.

HTTP 402. Your balance can't cover the request: add credits on the Credits page. A negative balance gives 402 even on :free models. If the error's limit_source names the key limit, raise the key's credit limit or wait for it to reset.

The model isn't found. The slug is misspelled, or it's a native Anthropic ID such as claude-sonnet-5-5. OpenRouter slugs include the author and use dots: anthropic/claude-sonnet-5.5. If MODEL names an Ollama model, your base URL still points at OpenRouter, or the other way round.

claude -p suddenly spends OpenRouter credits. The ANTHROPIC_* variables were exported in your shell. Run /status in Claude Code: if it shows https://openrouter.ai/api as the base URL, run unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY, remove them from your shell profile, and keep them in .env only.

Going further

  • OpenRouter or a local model?, the configuration pattern every answer uses
  • Local models with Ollama, the free alternative

Sources

  • API authenticationDocs · OpenRouterCreating a key with a credit limit, and using it as a bearer token or with the OpenAI SDK.
  • Anthropic Agent SDKDocs · OpenRouterThe three environment variables that point the Claude Agent SDK at OpenRouter.
  • LimitsDocs · OpenRouterPer-key credit limits, the free-model rate limits, and why a negative balance blocks even free models.
  • Data collectionDocs · OpenRouterWhat OpenRouter stores, and the two opt-in settings that are off by default.
  • Zero Data RetentionDocs · OpenRouterRestricting routing to providers that don't store your prompts, per model group or per request.

Exercises that use this

  • Measure the cost of difficult customersAgent Benchmarks and Simulations · Building user simulators
  • Port a Quillmoor task to Inspect and compare the logsAgent Benchmarks and Simulations · Task specs, harnesses and sandboxes
  • Run the same task through the Agent SDKAgent Foundations · What is an agent?
  • Same report through the Agent SDKAgent Foundations · Structured outputs and grounding
  • Build and test a session store adapterInfrastructure for Agents · State, sessions and checkpoints
  • Build and attack a per-matter memory-tool handlerAgent Memory · Files, skills and memory tools
  • Run the title-review agent on the SDKBuilding Agents with Claude and eve · The Claude Agent SDK
  • Build and break a contract-pack reader in eveBuilding Agents with Claude and eve · Agents as files with eve
  • Port the skill and subagent to eveBuilding Agents with Claude and eve · Skills and subagents
  • A solicitor-approved send in eveBuilding Agents with Claude and eve · Human approval for high-stakes actions
  • The same gate in the Agent SDKBuilding Agents with Claude and eve · Human approval for high-stakes actions
  • Route auth and tenant isolation, checkedBuilding Agents with Claude and eve · Shipping agents
  • Deploy to Vercel with a deployable modelBuilding Agents with Claude and eve · Shipping agents
  • Self-host an open parsing modelDocument Ingestion Pipelines · Parsing: text layers to vision models
  • Write five eve evals for the enquiries agentEvaluating and Scoring Agents · Code-based graders
  • Wire a judge into your eve evalsEvaluating and Scoring Agents · LLM-as-judge
  • Measure flakiness and compare two prompt versionsEvaluating and Scoring Agents · Statistics, trials and CI gates
  • Wire a two-tier eval gate into CIEvaluating and Scoring Agents · Statistics, trials and CI gates
  • Turn teacher trajectories into a TRL datasetFine-Tuning and RL for Agents · Training data from traces
  • DPO the free-text fields with on-policy pairsFine-Tuning and RL for Agents · Preference optimisation
  • KTO from thumbs, against DPO on the same budgetFine-Tuning and RL for Agents · Preference optimisation
  • GRPO the verifiable triage fields on a free GPUFine-Tuning and RL for Agents · GRPO in practice
  • Build and validate a composite triage rewardFine-Tuning and RL for Agents · Designing rewards
  • Compare a relative judge with a pointwise oneFine-Tuning and RL for Agents · Designing rewards
  • Add an open-weight model to the bake-offLLMs for Engineers · Choosing and comparing models
  • Build and validate a privacy-preserving topic mapHuman Feedback at Scale · Understanding usage at scale, privately
  • Scale the map with a local model and add differential privacyHuman Feedback at Scale · Understanding usage at scale, privately
  • Measure warm-then-fan-out on your subscriptionLLM Cost Engineering · Prompt caching as system design
  • Build a resumable batch extraction jobLLM Cost Engineering · Batch, flex and fast tiers
  • Distil a clause tagger and measure the gapLLM Cost Engineering · Distilling into small models
  • Fan out to eve subagents with structured resultsProduction Agent Systems · Multi-agent systems
  • An eve workflow tool that waits for a search resultProduction Agent Systems · Durable, long-running agents
  • Fixed recipients and a locked sandbox in eveProduction Agent Systems · Security and prompt injection
  • Audit hook and retention policy in eveProduction Agent Systems · Compliance and auditability
  • Session budgets and redacted exports in eveProduction Agent Systems · Observability and cost
  • An eval-gated model swap in eve CIProduction Agent Systems · Continuous improvement
  • DSPy: MIPROv2 versus GEPAPrompt and Context Optimisation · Automatic prompt optimisation
  • Gate prompt changes in CIPrompt and Context Optimisation · Model migration and regression suites
  • Measure truncation and quantisation on legal textRetrieval and Knowledge Systems · Embeddings in practice
  • Add a commercial embedder (stretch)Retrieval and Knowledge Systems · Embeddings in practice
  • Structure-aware and contextual chunking, measuredRetrieval and Knowledge Systems · Chunking structured documents
  • Tune an ANN index against a recall targetRetrieval and Knowledge Systems · Vector indexes and databases
  • Build and run a labelled query setRetrieval and Knowledge Systems · Measuring retrieval
  • Verify citations end to end (stretch)Retrieval and Knowledge Systems · Measuring retrieval
  • Build and measure a hybrid pipelineRetrieval and Knowledge Systems · Hybrid search and reranking
  • Compare single-shot, grep-only and full agentic retrievalRetrieval and Knowledge Systems · Agentic retrieval
  • Build scoped matter tools with the Agent SDK (stretch)Retrieval and Knowledge Systems · Agentic retrieval
  • Compare long context, RAG and a router on your questionsRetrieval and Knowledge Systems · Long context versus RAG
  • Log rate-limit headers and cache usageScaling AI Systems · Rate limits and quotas
  • A deferrable-work batch pipeline against a mockScaling AI Systems · Backpressure, load shedding and batching
  • Instrument and speed up a real agent turnScaling AI Systems · Latency engineering for agents
  • Does an open model vary the same way?Think Like a PhD Student · Designing the experiment

On this page

  • Before you start
  • 1. Create an account and buy credits
  • 2. Create a key with a credit limit
  • 3. Keep the key in the project's .env
  • 4. Check the privacy settings
  • 5. Know the free models and their limits
  • 6. Pick a model and connect your code
  • Claude Agent SDK and Anthropic SDK code
  • Everything else: the OpenAI SDK
  • Check it works
  • Troubleshooting
  • Going further
  • Sources
  • Exercises that use this