This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Install dependencies
pip install -r requirements.txt
# Run the proxy server
python opencode.pyServer starts on:
- API: http://localhost:4000
- Web Dashboard: http://localhost:8082
Configuration is managed through .env file (create from .env.example):
cp .env.example .envKey environment variables:
OPENCODE_PROXY- Proxy server URLOPENCODE_API_KEY- API key for OpenCode serviceOPUS_MAP_MODEL- Model for opus route (default:kimi-k2.6)SONNET_MAP_MODEL- Model for sonnet route (default:glm-5.1)HAIKU_MAP_MODEL- Model for haiku route (default:minimax-m2.5)
Optional server overrides:
OPENCODE_HOST- Bind address (default:0.0.0.0)OPENCODE_PORT- API port (default:4000)OPENCODE_WEB_PORT- Web UI port (default:8082)
opencode.py- Main FastAPI server with request routing and token tracking
__init__.py- Exports:API_BASE_OPENAI,API_BASE_ANTHROPIC,HOST,PORT,WEB_PORT,MODELS,ROUTES,get_model_config,API_KEY,PROXYsettings.py- Loads environment variables from.env, defines:PROXYandAPI_KEY(secrets)MODELS(model endpoints and protocols)ROUTES(model name mappings, built from env vars)get_model_config()- Returns merged config for a model
__init__.py- Exports:register_dashboard,log,RichLogHandler,build_display,start_input_threadapi.py- Dashboard API endpoints: stats, logs, history, static file servingdisplay.py- Rich terminal display: token usage table, log panel, keyboard input (j/k/g/G/arrows)
- Request comes to
/v1/messagesor/anthropic/v1/messages _route_for()maps model name to route config usingROUTESget_model_config()gets endpoint and protocol for the model- Request is forwarded to the appropriate endpoint (OpenAI or Anthropic protocol)
- Response is converted between Anthropic and OpenAI formats
/v1/messages/count_tokensestimates token count without forwarding
- Non-streaming: Uses actual
usagefrom API response - Streaming: Estimates input tokens, reads actual usage from SSE events when available
- Stored in SQLite database (
logs/requests.db) - Terminal display via Rich (
dashboard/display.py) - Web dashboard via API endpoints (
dashboard/api.py)
- Static files in
static/directory - Token usage stats and request history via API endpoints