pi Coding Agent API Setup

Configure pi, the extensible coding agent, with a TokenHub provider, environment-based API key, and compatible model.

What is pi, the coding agent?

pi is an extensible command-line coding agent with custom providers, models, and extensions for personal development workflows.

This pi agent guide adds TokenHub to models.json, keeps the API key in an environment variable, and declares model limits accurately before you select the provider in the pi CLI. pi belongs to the pi-mono project and supports custom providers, model capability declarations, and extensions. The models.json values are operational limits rather than decorative metadata: use verified context and output values, protect the API key with an environment variable, and validate extensions separately when they request additional tools.

pi's official Custom Models mechanism loads gateways and custom models from ~/.pi/agent/models.json. TokenHub uses the openai-completions API type. There is no generic “Add Provider” form here; this configuration file is the supported entry point.

Step 1: prepare a credential for models.json

In the same terminal that launches pi, set:

export TOKENHUB_API_KEY="sk-..."

In models.json, the value must be "$TOKENHUB_API_KEY" to read an environment variable. pi's documentation notes that an uppercase string without $ is treated as a literal key, not an environment-variable name.

Step 2: edit ~/.pi/agent/models.json

{
  "providers": {
    "tokenhub": {
      "baseUrl": "__API_BASE_URL__/v1",
      "api": "openai-completions",
      "apiKey": "$TOKENHUB_API_KEY",
      "models": [
        {
          "id": "YOUR_TOKENHUB_MODEL_ID",
          "name": "TokenHub / YOUR_TOKENHUB_MODEL_ID",
          "reasoning": false,
          "input": ["text"],
          "contextWindow": 128000,
          "maxTokens": 8192
        }
      ]
    }
  }
}

Correct each field from the TokenHub model page:

  • id is the model ID sent to the API and is required.
  • Set reasoning to true only for a model that supports extended reasoning.
  • input defaults to text. Use ["text", "image"] only for a model with image input.
  • Although contextWindow has a default, declare the model's real context window.
  • maxTokens is maximum output, not the context window.

TokenHub uses a Bearer token. If your pi version does not add the Authorization header automatically, add "authHeader": true at provider level.

Step 3: open /model to reload the file

Run /model inside a pi session. pi reloads models.json every time /model opens, so no restart is required. Choose tokenhub/YOUR_TOKENHUB_MODEL_ID, ask it to explain one file, and test tool calling only after that succeeds.

If the provider and model are displayed but not selectable, the file loaded but credentials did not resolve. Check the environment variable, or save the provider key with /login / auth.json.

Apply pi-specific compatibility overrides only when needed

Some OpenAI-compatible services reject the developer role or reasoning_effort. Add these flags only after the corresponding 400 error occurs:

"compat": {
  "supportsDeveloperRole": false,
  "supportsReasoningEffort": false
}

Do not disable capabilities preemptively. Once basic requests succeed, configure thinkingLevelMap, image input, sampling parameters, or pricing only for the exact model.

Troubleshoot according to pi's load behavior

  • Provider entirely absent from /model: check the file path, JSON syntax, baseUrl, and api.
  • Provider exists but model cannot be selected: $TOKENHUB_API_KEY did not resolve, or the provider lacks a saved credential.
  • 401: confirm the pi process inherited the variable and set authHeader: true if required.
  • 400 or early truncation: correct contextWindow, maxTokens, or compat; do not simply increase the numbers.
  • Change does not appear: reopen /model instead of relying on the old selection shown in the current session.

Official references

Choose a compatible pi model

WorkloadModel choiceWhy it fits
Complex coding tasksA tool-capable reasoning modelUse a model with reliable tool use, streaming, and the context length your workflow needs.
Daily implementationA balanced coding modelPrioritize reliable edits and useful latency for normal coding loops.
Background and subagent workA fast, lower-cost modelUse this for iterations, summaries, and simpler work only after validating tool use.
Browse TokenHub models and compatibility

pi Coding Agent provider FAQ

How are pi agent, pi Coding Agent, and pi-mono related?

pi-mono is the project repository containing several components, including the pi Coding Agent CLI. This page covers that coding agent, not Raspberry Pi.

Why does a pi custom provider require model limits?

pi uses context and output limits to shape requests and capabilities. Enter verified model specifications rather than copied or invented values.

Do pi extensions automatically use the TokenHub model?

They often follow the current session provider, but an extension can have separate requirements or settings. Review permissions and validate each extension safely.

Why can pi not find TOKENHUB_API_KEY?

Confirm models.json references the exact variable name and start pi from a new terminal that has loaded it. Do not store the real key in repository configuration.

How do I test a pi Coding Agent provider?

Select the provider and model, run a read-only code task, and check TokenHub logs before enabling edits, commands, or extensions.

References

This setup is based on TokenHub and official documentation, last verified on 2026-09-08.