Skip to content
Sign Up Free
Coding and development

Pi Agent API setup: Base URL, API key, and models

Connect Pi Agent to a third-party API with models.json, a custom Base URL, API key, and Model ID. Configure protocols, verify access, and fix common errors.

Pi Agent, the Pi terminal coding agent, connects to compatible third-party APIs through models.json. This guide uses BetterToken: set an API key, match the Base URL to the protocol, and specify a Model ID. For another service, use its URL, credentials, and model IDs.

Key settings

FieldValue
API KeyBetterToken API Key through the BETTERTOKEN_API_KEY environment variable
Base URLOpenAI protocol: https://bettertoken.ai/v1; Anthropic protocol: https://bettertoken.ai
ModelThe exact Model ID from the model plaza
Configuration file~/.pi/agent/models.json

Prerequisites

Sign up and get a BetterToken API Key

Already have an account? Sign in, top up, then create a key in the console.

Install

Use Pi’s current official npm installation command:

Terminal window
npm install -g --ignore-scripts @earendil-works/pi-coding-agent
pi --version

Manual setup

Set the API key

In the terminal that will start Pi, replace YOUR_API_KEY with your key:

macOS / Linux

Terminal window
export BETTERTOKEN_API_KEY="YOUR_API_KEY"

Windows PowerShell

Terminal window
$env:BETTERTOKEN_API_KEY="YOUR_API_KEY"

Get models and generate configuration

Click Get model list to fetch BetterToken’s current public catalog without entering an API key. Choose an API and models with tool calling, then copy a Model ID or download a generated models.json.

If models.json already exists, merge the generated bettertoken entry into your current configuration instead of replacing the file. The configuration uses the BETTERTOKEN_API_KEY variable set above; add any other model-specific settings you need manually.

Pi supports several protocols; choose one supported by both your model and endpoint. For this setup, we recommend Anthropic Messages for Claude, Responses for GPT when supported by the endpoint, and Chat Completions for other OpenAI-compatible models. The picker defaults to Chat Completions and filters by the APIs declared in the public catalog. If Responses does not list your model, use the manual example below for models with confirmed Responses support.

API

Add the URL and model

Create ~/.pi/agent/models.json. On Windows, the default path is %USERPROFILE%\.pi\agent\models.json. If the file exists, merge the bettertoken entry into its existing providers object.

As an alternative to the generated configuration, fill in this Responses API example manually. Replace YOUR_MODEL_ID with a model plaza ID; keep BETTERTOKEN_API_KEY as the environment-variable reference:

{
"providers": {
"bettertoken": {
"baseUrl": "https://bettertoken.ai/v1",
"api": "openai-responses",
"apiKey": "BETTERTOKEN_API_KEY",
"models": [{ "id": "YOUR_MODEL_ID" }]
}
}
}

For another API, change both baseUrl and api using a matching pair:

APIapibaseUrl
Responses APIopenai-responseshttps://bettertoken.ai/v1
Chat Completionsopenai-completionshttps://bettertoken.ai/v1
Anthropic Messagesanthropic-messageshttps://bettertoken.ai

Choose an API that supports tool calling for your model. For example, GPT-6.1 Sol uses Responses. Keep the Base URL as shown; Pi appends the request path.

Start Pi

From your project directory, use the Model ID you configured:

Terminal window
pi --provider bettertoken --model YOUR_MODEL_ID

Set the environment variable again in a new terminal, or persist it in your shell configuration.

Verify the connection

Run pi --list-models and confirm that bettertoken and your model appear. Then start Pi and send:

Reply with PI_OK only. Do not use tools.

After receiving PI_OK, try a small read-only task to verify tool calling.

Switch models

Add model entries to the models array, then run /model in Pi. Pi reloads models.json. Select a model and press Ctrl+S to save it as the default for new sessions. Use different local names for endpoints with different protocols, each with its own api and baseUrl.

Common errors

SymptomWhat to do
Model missing from the listCheck JSON syntax, the Model ID, and the environment variable; reopen /model.
401 or UnauthorizedCheck that the variable contains a valid BetterToken API Key and Pi starts from the same terminal.
404 or incorrect URLMatch api and baseUrl using the table. Remove manually appended /responses, /chat/completions, or /v1/messages.
Text works but tool calls failCheck model and API tool support; use openai-responses for models that require Responses.
Bash missing on WindowsInstall Git for Windows and reopen the terminal, or configure shellPath using Pi’s Windows guide.

Advanced setup

For images, reasoning levels, or longer context, set input, reasoning, contextWindow, and maxTokens according to the model’s actual capabilities. See the model selection guide.

Technical details

providers and --provider are Pi client configuration fields; bettertoken is the local name used here. Pi checks a CLI --api-key first, then saved auth.json credentials, then apiKey in models.json. If that local name has old saved credentials, remove the corresponding entry through /logout in Pi before reconnecting.

Official Pi references