pi-anyrouter
v0.3.2
Published
Unofficial pi provider extension for AnyRouter Claude Code and Codex Responses Lite routes
Maintainers
Readme
pi-anyrouter
A pi provider extension that adapts requests to the client-specific shapes accepted by AnyRouter.
It currently supports both Claude Code / Claude Agent SDK requests and Codex Responses Lite requests. It does not require a separate local relay process.
Status
Confirmed working with provider anyrouter using Claude and Codex model routes:
pi --model anyrouter/claude-opus-4-8 --no-session --no-tools -p "Reply with exactly OK"
pi --model anyrouter/gpt-5.6-sol --no-session --no-tools -p "Reply with exactly OK"Expected output:
OKWhat this package fixes
Some AnyRouter Claude endpoints, especially claude-opus-4-6, reject older or more generic Claude-style requests with errors like:
{"error":{"type":"new_api_error","message":"invalid claude code request (...)"},"type":"error"}This package selects an adapter by model family:
- Claude models use
POST /v1/messages?beta=truewith Claude Code headers, system blocks, metadata, and Anthropic SSE conversion. - Codex models use
POST /v1/responseswith the Codex Responses Lite headers/body shape and OpenAI Responses SSE conversion.
Features
- Registers provider:
anyrouter - Supports Claude Code and Codex Responses Lite request validation
- No local relay/proxy required
- Converts tool names to Claude Code naming
- Supports reasoning via
thinking+output_config - Built-in request/response debug dump
- Retries transient upstream failures like HTTP 520/502/503/504 automatically
- Packaged so it can be shared as a pi package
Install
Option A: local development checkout
Clone into a normal directory:
git clone https://github.com/xifan2333/pi-anyrouter.git
cd pi-anyrouterInstall as a pi package from the local path:
pi install .Or install directly from a path later:
pi install /absolute/path/to/pi-anyrouterOption B: direct git install
Once pushed to GitHub, install with:
pi install git:github.com/xifan2333/pi-anyrouterOr pin a ref/tag:
pi install git:github.com/xifan2333/pi-anyrouter@<tag>Option C: manual extension placement
If you only want the extension file layout, place it at either:
~/.pi/agent/extensions/anyrouter/index.ts.pi/extensions/anyrouter/index.ts
Then reload pi:
/reloadConfig
Create:
~/.pi/agent/anyrouter.json
Example:
{
"baseUrl": "https://anyrouter.top",
"apiKey": "YOUR_ANYROUTER_KEY_OR_ENV_NAME",
"models": [
{
"id": "claude-opus-4-8",
"name": "Claude Opus 4.8",
"reasoning": true,
"input": ["text"],
"cost": {
"input": 0,
"output": 0,
"cacheRead": 0,
"cacheWrite": 0
},
"contextWindow": 200000,
"maxTokens": 32000
}
]
}You can also override config values with environment variables:
PI_ANYROUTER_CC_CONFIGPI_ANYROUTER_CC_BASE_URLPI_ANYROUTER_CC_API_KEYPI_ANYROUTER_CC_MAX_RETRIES(default:10)PI_ANYROUTER_CC_STREAM_MODE(default:force)
Notes:
apiKeycan be a literal key or an environment variable name.- shell-command values like
"!command"are intentionally not supported. - this package reads
~/.pi/agent/anyrouter.jsonby default rather thanmodels.json. PI_ANYROUTER_CC_STREAM_MODE=autotries real SSE streaming first and falls back to the old non-stream JSON path if streaming fails before any content arrives.PI_ANYROUTER_CC_STREAM_MODE=forceuses SSE only;PI_ANYROUTER_CC_STREAM_MODE=offdisables SSE.
Use
After installation/configuration:
/reload
/modelChoose a configured model, for example:
anyrouter / claude-opus-4-8anyrouter / gpt-5.6-sol
Or run directly:
pi --model anyrouter/gpt-5.6-solDebugging
Enable request/response dumps:
PI_ANYROUTER_CC_DEBUG=1 pi --model anyrouter/gpt-5.6-sol -p "Reply with exactly OK"Optional custom debug directory:
PI_ANYROUTER_CC_DEBUG=1 \
PI_ANYROUTER_CC_DEBUG_DIR=/tmp/anyrouter-cc-debug \
pi --model anyrouter/gpt-5.6-sol -p "Reply with exactly OK"Default debug output directory:
.pi/anyrouter-cc-debug/
If AnyRouter intermittently returns HTTP 520 / Origin Error, this package retries transient upstream failures automatically with exponential backoff. You can tune the retry count with PI_ANYROUTER_CC_MAX_RETRIES.
The runtime uses SSE by default (PI_ANYROUTER_CC_STREAM_MODE=force). For troubleshooting, you can force old behavior with:
PI_ANYROUTER_CC_STREAM_MODE=off pi --model anyrouter/claude-opus-4-8 -p "Reply with exactly OK"Share as a pi package
This repo is already structured as a pi package through package.json:
{
"keywords": ["pi-package"],
"pi": {
"extensions": ["./index.ts"]
}
}That means users can install it with:
pi install git:github.com/xifan2333/pi-anyrouterPublish checklist
Publish to GitHub
git init
git add .
git commit -m "feat: add AnyRouter Claude and Codex adapters"
git branch -M main
git remote add origin [email protected]:xifan2333/pi-anyrouter.git
git push -u origin mainOptional npm publish
If you want npm distribution too:
npm publishThen users can install with:
pi install npm:pi-anyrouterImportant behavior
This package implements the Claude Code / Claude Agent SDK and Codex Responses Lite request shapes currently accepted by AnyRouter.
If AnyRouter changes its validation again, enable debug dumps and compare the request shape against fresh official client captures.
Credits
Based on pi-anyrouter-cc by zhenliangzhang, licensed under the MIT License.
This is an unofficial community project and is not affiliated with Anthropic, OpenAI, or AnyRouter.
License
MIT. See LICENSE for the original and modification copyright notices.
