Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

AgentRouter OpenCode Proxy

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.

Features

  • ✅ Injects the required User-Agent accepted by AgentRouter
  • ✅ Removes null request fields rejected by AgentRouter
  • ✅ Filters invalid billing.summary responses
  • ✅ Cleans problematic SSE streaming events
  • ✅ Prevents OpenCode Zod validation errors
  • ✅ Supports both streaming and non-streaming requests
  • ✅ No modifications required to OpenCode

Why?

AgentRouter is not 100% compatible with the official OpenAI API.

Common issues include:

  • 401 unauthorized client detected
  • expected object, received null
  • billing.summary
  • Type validation failed
  • SSE events that are not valid OpenAI responses

This proxy transparently fixes these issues so OpenCode works normally.


Requirements

  • Python 3.10+
  • AgentRouter API Key
  • OpenCode

Installation

Clone the repository:

git clone https://github.com/YOUR_USERNAME/agentrouter-opencode-proxy.git
cd agentrouter-opencode-proxy

Configuration

Set your API key before starting.

Windows PowerShell

$env:AGENTROUTER_API_KEY="YOUR_API_KEY"

Linux/macOS

export AGENTROUTER_API_KEY="YOUR_API_KEY"

Optional:

AGENTROUTER_UPSTREAM=https://agentrouter.org
AGENTROUTER_DEBUG=1

Running

python agentrouter-proxy.py

Default address:

http://127.0.0.1:4182

OpenCode Configuration

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.


What the proxy fixes

User-Agent Injection

Automatically sends

User-Agent: opencode/<version>

so AgentRouter accepts the client.


Request Cleanup

Removes fields like

{
  "response_format": null
}

that AgentRouter rejects.


Streaming Cleanup

Filters invalid SSE events such as

{
  "object":"billing.summary",
  "billing":{...}
}

before they reach OpenCode.


Non-stream Responses

Filters invalid metadata responses that would otherwise cause validation errors.


Debug Mode

Enable logging:

Windows

$env:AGENTROUTER_DEBUG="1"

Linux/macOS

export AGENTROUTER_DEBUG=1

Automatic Startup (Windows)

You 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.


Project Structure

.
├── agentrouter-proxy.py
├── README.md
└── LICENSE

Compatibility

Tested with:

  • OpenCode
  • AgentRouter

Should also work with any OpenAI-compatible client that communicates through the OpenAI API.


License

MIT

About

Local proxy that normalizes partially-OpenAI-compatible upstreams (FuturePPO, AgentRouter, NewAPI, RoutLLM) into strict OpenAI API responses. Fastify + TypeScript + Undici, SSE streaming, response cleaning + repair.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages