Skip to content

Repository files navigation

LocalCode Middleware Server

A lightweight OpenAI-compatible proxy for OpenCode that routes requests to OpenCode Zen (free GLM 4.7) or local llama.cpp. Powered by LiteLLM.

Features

  • OpenAI-Compatible API: /v1/chat/completions endpoint
  • Free Models: GLM 4.7, Big Pickle, Grok Code, Alpha GD4 via OpenCode Zen
  • Local Models: llama.cpp via local server
  • Auto-Retries: Built-in retry logic for rate limits (429)
  • Connection Pooling: Efficient httpx client reuse
  • Structured Logging: Request/response/event logging with category markers

Installation

# Install with poetrycd localcode
poetry install
# Or with pip
pip install litellm pyyaml

Configuration

Edit config.yaml to configure models and settings:

model_list:
- model_name: glm-4.7-freelitellm_params:
model: glm-4.7-freeapi_base: https://opencode.ai/zen/v1api_key: "public"# Free tier - no key neededmax_retries: 3retry_after: 10# Wait 10s on 429 before retrytimeout: 300.0litellm_settings:
default_max_retries: 3default_retry_after: 10allow_auth_on_null_key: true

Environment Variables

VariableDefaultDescription
LITELLM_CONFIGconfig.yamlPath to config file
LITELLM_HOST0.0.0.0Server host
LITELLM_PORT4242Server port
LITELLM_LOGLEVELINFOLogging level

Running

# Start with default config
poetry run python main.py
# Or use litellm directly
litellm --config config.yaml
# Custom port
poetry run python main.py --port 8080
# Custom config
poetry run python main.py --config my-config.yaml

Switching Backends

Cloud (OpenCode Zen - Default)

model_list:
- model_name: glm-4.7-freelitellm_params:
api_base: https://opencode.ai/zen/v1api_key: "public"

Local (llama.cpp)

model_list:
- model_name: qwen3-coder:a3blitellm_params:
api_base: http://localhost:8080/v1api_key: "no-key-required"max_retries: 0# Local - no retries needed

OpenCode Configuration

Add to your opencode.json:

{
"$schema": "https://opencode.ai/config.json",
"provider": {
"localcode": {
"npm": "@ai-sdk/openai-compatible",
"name": "localcode",
"options": {
"baseURL": "http://localhost:4242/v1"
},
"models": {
"glm-4.7-free": {
"name": "GLM-4.7 Free",
"limit": {
"context": 204800,
"output": 131072
}
}
}
}
}
}

API Endpoints

EndpointDescription
POST /v1/chat/completionsChat completions (OpenAI-compatible)
GET /v1/modelsList available models
GET /healthHealth check

Log Output

The middleware prints structured logs:

Request

================================================================================
[REQUEST] 12:34:56
Model: glm-4.7-free
Stream: False
Messages count: 1
================================================================================

Response

--------------------------------------------------------------------------------
[RESPONSE] 12:34:58
Content: I'll help you edit the function...
Finish reason: stop
Usage - prompt: 150, completion: 45, total: 195
[Cached Tokens: 12]
--------------------------------------------------------------------------------

Tool Call

[Tool Call] edit
[Tool Call] read

Reasoning (GLM 4.7)

[STREAM CHUNK] 12:34:57 [REASONING] Let me think about this...
[STREAM CHUNK] 12:34:58 [REASONING] 1+1=2, so 2+2=4
[STREAM CHUNK] 12:34:59 4

Architecture

Refactored from custom Python implementation to LiteLLM proxy:

BeforeAfter
6 modules, ~736 lines3 files, ~50 lines
Custom HTTP clientBuilt-in LiteLLM router
Custom SSE parsingCustomStreamWrapper
No retry logicmax_retries, retry_after
No connection poolinghttpx client reuse

Files

  • config.yaml - LiteLLM model configuration
  • main.py - Server entrypoint
  • logging_callbacks.py - Custom event logging
  • opencode.json - OpenCode provider config

License

Same as parent OpenCode project.

About

LocalCode is a middleware that sits between coding agents (e.g. opencode) and various local LLM backends. It provides request interception, intelligent caching, and request transformation to optimize local LLM execution speed and code quality.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages