Claude Code × OpenRouter Free モデルで月0円AI開発環境を構築する5つの設定 #AI - Qiita
@locallab

5 Settings for Building a $0/Month AI Development Environment with Claude Code × OpenRouter Free Models

  • Claude
  • AI
  • OpenRouter
0
Posted at 2026-11-11

TL;DR

  • You can use free models via OpenRouter simply by replacing Claude Code's ANTHROPIC_API_KEY
  • You can instantly switch to Qwen3, Gemini Flash, Llama4, and other models with the claude --model flag
  • Introducing three rotation strategies to avoid free-tier rate limits
  • Monthly cost: $0 (when using only free models)

Background

The cost of AI coding assistants is becoming a significant expense for individual developers and small teams. Claude Code is powerful, but the typical setup involves a claude.ai Pro subscription ($20/month and up) plus pay-as-you-go API charges.

However, OpenRouter offers many free models with the :free suffix, such as google/gemini-flash-1.5-8b:free and qwen/qwen3-235b-a22b:free. Because Claude Code supports custom endpoint configuration, combining them enables a zero-cost AI development environment.

This article explains the actual setup procedure, model selection guidelines, and methods for dealing with rate limits.

Prerequisites

Item Version / Requirements
Claude Code v1.x (npm: @anthropic-ai/claude-code)
Node.js 18+
OpenRouter account Free registration (https://openrouter.ai)
OS macOS / Linux / WSL2

1. Understand the Basic Structure of OpenRouter

OpenRouter is a proxy service that functions as an OpenAI-compatible API. The endpoints are as follows:

Base URL: https://openrouter.ai/api/v1 Authentication: Authorization: Bearer sk-or-v1-xxxx Model: provider/model-name:variant

Free models have the :free suffix. As of May 2026, the major free models are as follows:

Model ID Features Context
google/gemini-flash-1.5-8b:free Fast, lightweight, and suited for code completion 1M tokens
google/gemini-2.0-flash-exp:free Gemini 2.0 series, multimodal 1M tokens
qwen/qwen3-235b-a22b:free Extremely large-scale, high-accuracy code generation 128K tokens
meta-llama/llama-4-scout:free Meta's latest model, with strong instruction-following capabilities 512K tokens
mistralai/mistral-small-3.1-24b-instruct:free Mistral, lightweight and low-latency 128K tokens

Note: Since free model availability may change, check the latest list at https://openrouter.ai/models?order=newest&supported_parameters=free .

2. Configuring a Custom Endpoint for Claude Code

Claude Code allows you to change the destination of the Anthropic API through environment variables.

2-1. Basic Settings (Append to the Shell Configuration File)

# ~/.bashrc or ~/.zshrc # OpenRouter API key (issued at openrouter.ai)export ANTHROPIC_API_KEY="sk-or-v1-<YOUR_OPENROUTER_KEY>" # Point the Anthropic API to OpenRouter export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1" # Set the default model to a free model export CLAUDE_MODEL="google/gemini-flash-1.5-8b:free"
source ~/.zshrc

2-2. Verification

claude --model "google/gemini-flash-1.5-8b:free" "Hello, what model are you?"

If a response is returned normally, the setup is complete.

2-3. Project-specific settings (using direnv)

If you want to use different models across multiple projects, direnv is useful:

# Create .envrc in the project root cat > .envrc << 'EOF' export ANTHROPIC_API_KEY="sk-or-v1-<YOUR_OPENROUTER_KEY>" export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1" export CLAUDE_MODEL="qwen/qwen3-235b-a22b:free" EOF direnv allow .

3. Three limitations of free models and how to handle them

Free models have limitations. Be aware of them for practical use.

Limitation 1: Rate limits (RPM/RPD)

Free models often have limits of around 20 RPM (Requests Per Minute) and 200 RPD (Requests Per Day).

Solution: Shell wrapper with exponential backoff

#!/bin/bash # claude-free.sh: Automatically retry when rate-limited MAX_RETRIES=5 RETRY_DELAY=10 for i in $(seq 1 $MAX_RETRIES); do OUTPUT=$(claude "$@" 2>&1) EXIT_CODE=$? if echo "$OUTPUT" | grep -q "rate_limit\|429\|Too Many Requests"; then echo "[claude-free] Rate limited. Waiting ${RETRY_DELAY}s... (attempt $i/$MAX_RETRIES)" sleep $RETRY_DELAY RETRY_DELAY=$((RETRY_DELAY * 2)) # Exponential backoff else echo "$OUTPUT"exit $EXIT_CODE fi done echo "[claude-free] Max retries exceeded." exit 1
chmod +x claude-free.sh alias claude="~/scripts/claude-free.sh"

Constraint 2: Context length limit

Qwen3-235b supports 128K tokens, but this may not be enough for large codebases.

Solution: Reduce context with .claudeignore

# .claudeignore node_modules/ dist/ build/ *.lock *.log .git/ coverage/

Simply placing this file in the project root allows Claude Code to automatically exclude the specified files.

Constraint 3: Model uptime

Free models may experience provider congestion depending on the time of day, resulting in more timeouts.

Solution: Model rotation script

#!/bin/bash # rotate-model.sh: If one fails, try the next free model MODELS=( "google/gemini-flash-1.5-8b:free" "google/gemini-2.0-flash-exp:free" "qwen/qwen3-235b-a22b:free" "meta-llama/llama-4-scout:free" "mistralai/mistral-small-3.1-24b-instruct:free" ) for MODEL in "${MODELS[@]}"; do echo "[rotate] Trying model: $MODEL"OUTPUT=$(claude --model "$MODEL" "$@" 2>&1) EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ] && ! echo "$OUTPUT" | grep -q "error\|Error"; then echo "$OUTPUT" exit 0 fi echo "[rotate] $MODEL failed, switching..." sleep 2 done echo "[rotate] All free models failed." exit 1

4. Model Selection Guide by Use Case

Not all free models are equally capable. Using them according to their intended purposes will improve quality.

4-1. Code Completion and File Editing → Gemini Flash 1.5 8B

It responds quickly and is well suited for short code modifications at the sed/awk level. It is ideal for the “diff generation” task, which is called most frequently in Claude Code.

export CLAUDE_MODEL="google/gemini-flash-1.5-8b:free" claude "Remove all console.log statements from this file"

4-2. Design/architecture consultation → Qwen3-235B

High reasoning quality as a large-scale model. It is well suited to tasks requiring thought, such as “How should this code be redesigned?” and “What is the time complexity of this algorithm?”

export CLAUDE_MODEL="qwen/qwen3-235b-a22b:free" claude "Propose a design for splitting this React component according to atomic design"

4-3. Long-form code generation/complex refactoring → Llama 4 Scout

Its long context of 512K tokens is highly useful. It is well suited for use cases where it reads an entire repository and produces an “overall optimal refactoring plan.”

export CLAUDE_MODEL="meta-llama/llama-4-scout:free" claude "Read everything under src/ and list the dependency-related issues"

5. Automate model-selection hints with CLAUDE.md

If you place CLAUDE.md in the project root, Claude Code will read it when the session starts. Writing model-selection instructions there can help the AI itself choose an appropriate model.

# Project: my-app ## AI Development Rules - Minor edits/formatting fixes: Gemini Flash 8B recommended - Design/review/explanation: Qwen3-235B recommended - Large-scale codebase scanning: Llama4 Scout recommended - Paid models (claude-3-5-sonnet, etc.) should be used only for important decisions ## Coding Conventions - TypeScript strict mode - Comments in Japanese - Tests: Vitest

Result: Cost comparison

Configuration Estimated monthly cost
Claude Pro + pay-as-you-go API $20–$80
Pay-as-you-go OpenAI GPT-4o (medium-scale use) $15–$50
OpenRouter free models only $0
OpenRouter free + paid models mixed $1–$10 (pay only for important tasks)

Summary

  1. Simply set ANTHROPIC_BASE_URL to https://openrouter.ai/api/v1, and Claude Code will run through OpenRouter
  2. Models with the :free suffix enable a fully functional AI development environment for ¥0 per month
  3. Handle rate limits with an exponential backoff wrapper and model rotation
  4. Use Gemini Flash (speed), Qwen3-235B (quality), and Llama4 (long-form text) according to your needs
  5. A hybrid strategy—falling back to paid models only for production use or important decisions—is the practical solution

As of 2026, free models have reached a practical level of quality and are comparable to paid models when generating boilerplate, adding tests, and performing small-scale refactoring. Start by trying them with just 10 minutes of setup.

Reference links

✍️ Author of this article: Jimolab Co., Ltd.

Jimolab develops many AI-powered SaaS products based in Hachioji. The technical validation described in this article is also a byproduct of that development process.

If you found this interesting, please also follow us on our various social media platforms!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
Sign upLogin