Ø THE AI SERVER
πŸ‡ΊπŸ‡Έ EN | πŸ‡ͺπŸ‡Έ ES | πŸ‡΅πŸ‡Ή PT
← Back to Insights
Model Routing · Zero-Cost Infrastructure

Connecting Claude Code to Custom Proxies & Local Gateways

A
Written by Akshat
Founder, The AI Server · Autonomous Systems Engineer
OmniRouter multi-node routing network powering Claude Code

⚑ Quick Summary: What is this guide about?

Anthropic's terminal agent, Claude Code (@anthropic-ai/claude-code), offers exceptional autonomous coding capabilities. However, because it reads repository files and iterates through multi-step terminal loops, token usage accumulates rapidly. In this technical guide, we demonstrate how to connect Claude Code to a local gateway daemon (such as OmniRoute or LiteLLM), configure prompt caching to reduce token recomputation by up to 90%, and set up local offline fallback models with Ollama.

What Exactly is Claude Code & Why Does It Burn Tokens?

If you are a beginner, think of Claude Code not as a regular web chatbot like ChatGPT, but as an autonomous software engineer operating inside your terminal. When you instruct it:

"Fix the race condition in the WebSocket handler, write unit tests with 90% coverage, and verify they pass."

Claude Code doesn't just print code suggestions; it autonomously:

  1. Explores your project directory structure and reads relevant source files into memory.
  2. Parses error logs, compiler warnings, and test reports.
  3. Directly edits code across multiple files on your disk.
  4. Executes terminal commands like npm test, pytest, or cargo test.
  5. Inspects test failures, debugs its own edits, and repeats the loop until your build is completely green.

Because every single cycle resends tens of thousands of tokens of codebase context back to the model, an engineer working 4 hours a day can easily burn millions of tokens. Without a smart gateway, the API cost adds up rapidly.

What is OmniRouter (OmniRoute)?

OmniRouter (packaged as the local gateway daemon omniroute) is an intelligent AI proxy and load balancer. Instead of your terminal sending requests straight to Anthropic's paid API, Claude Code routes its requests to your local OmniRoute server (or a shared cloud gateway). OmniRoute then translates and routes those requests across a pool of connected model providers.

OmniRoute AI Gateway web dashboard running on localhost:20128
Figure 1: The OmniRoute AI Gateway Overview Dashboard running locally at http://localhost:20128, monitoring active providers, token savings (98.4%), and average request latency.

As shown in the dashboard above, OmniRoute delivers several game-changing superpowers for developers:

How Intelligent Gateways Optimize Performance & Latency

Local proxy gateways provide several substantial engineering advantages for CLI coding agents:

OmniRoute Providers and Gateway Management Screen
Figure 2: OmniRoute Providers & Gateway Screen, displaying connection health, request latency, and local endpoint routing.
  1. Prompt Caching & Context Reuse: Claude Code repeatedly resends system prompts and repository maps. An intelligent gateway caches these static prefix tokens, slashing latency and reducing token consumption by up to 90%.
  2. Dynamic Model Tiering: Route lightweight routine tasks (like directory listing and code formatting) to fast local models, reserving frontier reasoning models for complex architectural refactoring.
  3. Offline Local Fallback: If upstream network connectivity drops or third-party cloud APIs experience high latency, the gateway can automatically fall back to your local Ollama instance (e.g., Qwen 2.5 Coder).
  4. Rate Limit Queueing & Throttling: Rather than crashing the terminal session with HTTP 429 errors, the gateway queues requests and applies exponential backoff in compliance with provider guidelines.
πŸ’‘ Engineering Best Practice: Always set reasonable request rate limits and timeout parameters in your local proxy configuration to avoid unexpected throttling.

1 System Prerequisites & Environment Setup

Before connecting Claude Code, ensure your system has Node.js (v18 or newer) installed. Open your terminal:

Verify your Node.js and npm versions:

bash β€” check environment
node -v
# Example output: v20.12.0 (Must be v18.0.0 or higher)

npm -v
# Example output: 10.5.0

2 Installing Claude Code CLI

You can install Claude Code globally using Anthropic's official script or standard npm:

bash β€” install claude code
# Method A: Official curl installer (Mac / Linux)
curl -fsSL https://claude.ai/install.sh | bash

# Method B: Global npm install (Cross-platform)
npm install -g @anthropic-ai/claude-code

# Verify the binary is ready:
claude --version

3 Starting Your OmniRoute Gateway & Dashboard

If you have the omniroute gateway installed on your machine, you can launch the background server and open the web dashboard with these simple commands:

bash β€” start omniroute daemon
# 1. Start the OmniRoute gateway daemon
omniroute serve --daemon

# 2. View the dashboard URL
omniroute dashboard --url
http://localhost:20128

# 3. Check live gateway status and connected models:
omniroute models

Now open your browser and navigate to http://localhost:20128/dashboard. From the left sidebar:

4 One-Click Auto Configuration: omniroute setup-claude

The easiest way to connect Claude Code is using OmniRoute's built-in profile generator. It talks directly to your running gateway, fetches all available models, and automatically generates ready-to-use profiles in your ~/.claude/profiles/ folder:

bash β€” omniroute auto profile generator
# Automatically generate Claude Code profiles for all connected models
omniroute setup-claude

OmniRoute β†’ Claude Code profile generator
Connecting to http://localhost:20128 …
Received 102 models from http://localhost:20128
  βœ“ profiles/claude-sonnet-4-6/settings.json  (claude-sonnet-4-6)
  βœ“ profiles/claude-opus-4-6/settings.json    (claude-opus-4-6-thinking)
  βœ“ profiles/gemini-3-1-pro/settings.json     (gemini-3.1-pro)
  βœ“ profiles/kimi-k2-7/settings.json          (kimi-k2.7-code)
  
Profiles written to ~/.claude/profiles/

Once generated, you can launch Claude Code pointed at any model profile with a single command:

bash β€” launch with native profile
# Launch Claude Code directly through OmniRoute!
omniroute launch --profile claude-sonnet-4-6

5 Manual Environment Variable Setup (Universal Method)

If you prefer standard environment variables or are connecting to a remote OmniRouter gateway, Claude Code natively supports redirection via two environment variables:

bash / zsh β€” manual environment configuration
# 1. Point base URL to your local OmniRoute instance (or remote cloud proxy)
export ANTHROPIC_BASE_URL="http://localhost:20128/v1"

# 2. Enter your OmniRoute gateway access token
export ANTHROPIC_AUTH_TOKEN="sk-omniroute-your-gateway-key"

# 3. Launch Claude Code
claude
πŸͺŸ Windows PowerShell:
$env:ANTHROPIC_BASE_URL="http://localhost:20128/v1"
$env:ANTHROPIC_AUTH_TOKEN="sk-omniroute-your-gateway-key"
claude

6 Client Launcher & Environment Script

To launch Claude Code reliably with your local gateway configuration, create a dedicated launcher script:

bash β€” claude-launcher.sh (Gateway Client Setup)
#!/usr/bin/env bash
# Save to ~/.local/bin/claude-gateway and make executable: chmod +x ~/.local/bin/claude-gateway

# Point to your local gateway server
export ANTHROPIC_BASE_URL="http://localhost:20128/v1"
export ANTHROPIC_AUTH_TOKEN="sk-omniroute-your-gateway-token"

echo "[*] Connected to Local AI Gateway ($ANTHROPIC_BASE_URL)"
exec claude "$@"

Optimize Your Claude Code Setup with Our Production Cheatsheet πŸ’‘

Looking for verified configurations for LiteLLM, OmniRoute, and local Ollama bridges? Download our production-tested developer cheatsheet containing environment variables, prompt caching rules, and context pruning configurations:

βœ“ Cheatsheet sent! Check your inbox for the complete proxy configuration guide.

Verifying Your Setup: Real Terminal Test Run

To verify everything is working, open any coding repository and run a sample prompt:

claude-code β€” live execution trace
> claude "Audit our package.json, find outdated dependencies, and create a PR branch."

● Reading package.json and lockfile...
● Connected via OmniRoute Gateway [Latency: 172ms | Route: Optimal]
● Checking npm registry for updates...
● Found 3 patch updates. Creating git branch 'chore/update-deps'...
● Ran test suite: 28/28 tests passing!
● Task completed successfully! Total API cost: $0.00

Troubleshooting & Common Beginner Mistakes

Problem / Error Underlying Cause Immediate Solution
command not found: claude Global npm bin folder not in your system $PATH Run npm config get prefix and add the /bin path to your ~/.bashrc or ~/.zshrc.
401 Authentication Required Missing or malformed gateway token Verify your ANTHROPIC_AUTH_TOKEN has no extra spaces. If using OmniRoute, check your client API key in omniroute keys list.
429 Rate Limit Exceeded Upstream model quota or request velocity limit reached Enable exponential backoff in your gateway settings, or configure a fallback to a local offline Ollama model.
Connection Refused (Port 20128) OmniRoute daemon is not running in the background Run omniroute serve --daemon to start the gateway server.
Session resets on new terminal tab Variables were only exported in a temporary subshell Add the export ANTHROPIC_BASE_URL=... lines directly into your ~/.zshrc or ~/.bashrc file.

Need Enterprise AI Architecture or Private Local LLM Rigs?

At The AI Server, we design private self-hosted coding agents, secure LLM routers, and developer automation engines for high-growth engineering teams.

Schedule an Engineering Call with Akshat β†’

Explore More Engineering Guides