A lightweight local HTTP proxy that makes AgentRouter work seamlessly with OpenCode and other OpenAI-compatible clients.
The proxy fixes several compatibility issues between AgentRouter and the AI SDK used by OpenCode.
- ✅ Injects the required
User-Agentaccepted by AgentRouter - ✅ Removes
nullrequest fields rejected by AgentRouter - ✅ Filters invalid
billing.summaryresponses - ✅ Cleans problematic SSE streaming events
- ✅ Prevents OpenCode Zod validation errors
- ✅ Supports both streaming and non-streaming requests
- ✅ No modifications required to OpenCode
AgentRouter is not 100% compatible with the official OpenAI API.
Common issues include:
401 unauthorized client detectedexpected object, received nullbilling.summaryType validation failed- SSE events that are not valid OpenAI responses
This proxy transparently fixes these issues so OpenCode works normally.
- Python 3.10+
- AgentRouter API Key
- OpenCode
Clone the repository:
git clone https://github.com/YOUR_USERNAME/agentrouter-opencode-proxy.git
cd agentrouter-opencode-proxySet your API key before starting.
$env:AGENTROUTER_API_KEY="YOUR_API_KEY"export AGENTROUTER_API_KEY="YOUR_API_KEY"Optional:
AGENTROUTER_UPSTREAM=https://agentrouter.org
AGENTROUTER_DEBUG=1
python agentrouter-proxy.pyDefault address:
http://127.0.0.1:4182
Instead of pointing OpenCode directly to AgentRouter:
https://agentrouter.org/v1
Use:
http://127.0.0.1:4182/v1
No other configuration changes are required.
Automatically sends
User-Agent: opencode/<version>
so AgentRouter accepts the client.
Removes fields like
{
"response_format": null
}that AgentRouter rejects.
Filters invalid SSE events such as
{
"object":"billing.summary",
"billing":{...}
}before they reach OpenCode.
Filters invalid metadata responses that would otherwise cause validation errors.
Enable logging:
$env:AGENTROUTER_DEBUG="1"export AGENTROUTER_DEBUG=1You can configure Windows Task Scheduler to automatically start the proxy when you log in.
Create a small batch file that launches the proxy with your API key and configure Task Scheduler to execute it at logon.
.
├── agentrouter-proxy.py
├── README.md
└── LICENSE
Tested with:
- OpenCode
- AgentRouter
Should also work with any OpenAI-compatible client that communicates through the OpenAI API.
MIT