pi-provider-newapi
pi provider extension for self-hosted NewAPI AI gateway — dynamic model discovery with cost calculation from ratio_config
Package details
Install pi-provider-newapi from npm and Pi will load the resources declared by the package manifest.
$ pi install npm:pi-provider-newapi- Package
pi-provider-newapi- Version
0.3.0- Published
- Jun 5, 2026
- Downloads
- not available
- Author
- ttimasdf
- License
- MIT
- Types
- extension
- Size
- 34 KB
- Dependencies
- 0 dependencies · 2 peers
Pi manifest JSON
{
"extensions": [
"./src/index.ts"
]
}Security note
Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.
README
pi-provider-newapi
pi coding agent provider extension for self-hosted NewAPI AI gateways.
Registers a single newapi provider with dynamic model discovery, automatic cost calculation, and backend routing based on each model's enriched API metadata:
| Recommended API | Endpoint |
|---|---|
openai-completions, openai-responses |
{baseUrl}/v1 |
anthropic-messages |
{baseUrl} |
Installation
This is a pi coding agent provider extension. It has no runtime dependencies — only peer dependencies on pi itself.
pi install npm:pi-provider-newapi
Or install from git:
pi install git:github.com/ttimasdf/pi-provider-newapi
The extension is auto-discovered via the pi.extensions field in package.json — no extra configuration is needed after install.
Quick Start
Option A: Environment variables (fast path)
export NEWAPI_BASE_URL=https://ai.your-gateway.com
export NEWAPI_API_KEY=sk-your-api-key
The provider registers automatically on startup with discovered models.
Option B: Interactive /login (persist key)
export NEWAPI_BASE_URL=https://ai.your-gateway.com
pi> /login
Run /login in the pi session. pi will interactively prompt for the provider, and for the API key (sk-your-api-key). The key is saved to <agentDir>/auth.json by pi's built-in credential store. On next load, the extension reads it automatically.
Configuration
Base URL and model metadata are stored in <agentDir>/extensions/provider-newapi.json. The API key is managed separately by pi's built-in <agentDir>/auth.json (set via /login or NEWAPI_API_KEY env var).
<agentDir> defaults to:
| OS | Path |
|---|---|
| Linux / macOS | ~/.pi/agent |
| Windows | %USERPROFILE%\.pi\agent |
{
"baseUrl": "https://ai.your-gateway.com",
"modelInfo": {
"unknown-model-id": {
"api": "anthropic-messages",
"reasoning": false,
"input": ["text"],
"contextWindow": 128000,
"maxTokens": 4096
}
}
}
On load, if NEWAPI_BASE_URL differs from stored, the base URL is updated. If NEWAPI_BASE_URL is not set, a warning is printed.
modelInfo entries are auto-generated for models not found in the built-in model database. Edit them to adjust api, reasoning, input types, contextWindow, or maxTokens. Optionally add thinkingLevelMap (e.g. { "xhigh": "max" }). When a previously unknown model later becomes known, the config entry is removed and upstream values take precedence, with a warning showing any differences between the config and upstream values.
How It Works
- Config reconciliation — reads stored key and base URL; syncs with
NEWAPI_BASE_URLenv var - Ratio config fetch — fetches
GET /api/ratio_config(no auth required) for NewAPI'smodel_ratio,completion_ratio,cache_ratio, andcreate_cache_ratiomaps. This step is best-effort — if it fails, costs simply report as0 - Model discovery — fetches
GET /v1/modelsfrom the gateway (requires API key) - Ratio matching — matches each model ID against ratio config keys using a three-step fallback: exact match → case-insensitive match → prefix match. This handles NewAPI's inconsistent naming (e.g. version tags, mixed casing)
- Model enrichment — matches discovered models against built-in model data from multiple providers, in this priority order:
deepseek,zai,google,anthropic,minimax,moonshotai,xiaomi,openai,vercel-ai-gateway. Models whose built-in API is not supported by NewAPI (openai-completions,openai-responses, oranthropic-messages) are skipped during lookup construction. Model IDs are normalized by stripping anyprovider/prefix and converting to lowercase. Earlier providers win when the same model appears in more than one source. Enrichment populatesapi,contextWindow,maxTokens,reasoning,thinkingLevelMap,inputtypes, and compatibility settings. During enrichment,supportsDeveloperRoleis set totrueforanthropicandopenaisources andfalsefor all others. Unknown models fall back to defaults (anthropic-messages, 128K context / 4096 max output tokens / text-only) - Cost calculation — converts from NewAPI quota to USD per million tokens. Based on NewAPI's formula
Quota = (Input + Output × CompletionRate) × ModelRate × GroupRatewith1 USD = 500,000 quota:cost.input = modelRatio × 2cost.output = modelRatio × completionRatio × 2cost.cacheRead = modelRatio × cacheRatio × 2cost.cacheWrite = modelRatio × createCacheRatio × 2
- Backend routing — each registered model carries its enriched
apiand endpoint, so pi routes through the appropriate built-in API handler instead of guessing from model ID prefixes - Model info templates — unknown models (not in built-in data) get a template added to
provider-newapi.jsonundermodelInfofor manual editing. When a previously unknown model later becomes known, the template is removed automatically and upstream values are used, with a warning logged showing any differences between config and upstream values
Graceful Fallback
If model discovery fails (network error, auth failure, etc.), the provider falls back to an unconfigured state:
- Registers a placeholder model
newapi/unconfigured - If an API key exists in
auth.json, prompts user to run/reloadthen/model - If no key exists, prompts user to run
/login
This ensures pi never crashes on startup — it always presents a usable (if inert) provider.
Requirements
- pi coding agent
- NewAPI gateway
- Built-in model metadata from the supported upstream providers listed above
- For cost tracking:
ExposeRatioEnabledmust betruein gateway Settings → Operation → Ratio - API key with access to models via the gateway
Without ratio_config
If the gateway has ExposeRatioEnabled set to false, the extension still works — all costs report as 0 (usage tracking disabled). Model discovery and routing are unaffected.