Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pi Agent Authentication and Model Configuration

Pi Agent Authentication and Model Configuration

Pi Agent supports two authentication methods: subscription login and API Key. This chapter explains in detail how to configure them so Pi Agent connects to AI models.


Authentication Method Overview

Authentication method Operation method Applicable scenarios
Subscription login (OAuth) After starting pi, run /login Already have a subscription such as Claude Pro/Max, ChatGPT Plus/Pro, or GitHub Copilot
API Key environment variable Set the environment variable and then start pi Have an API Key and want to persist it in shell config
API Key credential file Use /login to select an API Key provider for storage Have an API Key and want Pi Agent to manage it centrally

Method 1: Subscription login (recommended)

If you already subscribe to Claude Pro/Max, ChatGPT Plus/Pro, or GitHub Copilot, this is the most convenient method.

After starting Pi Agent, enter the following command:



/login

Then select your subscription provider from the menu that appears:

Provider Requirement Description
Claude Pro/Max Anthropic subscription account Billed using extra usage, not counting against the plan quota
ChatGPT Plus/Pro (Codex) OpenAI Plus or Pro subscription Codex for OSS officially recognized by OpenAI
GitHub Copilot GitHub Copilot subscription Supports github.com and enterprise servers
xAI (Grok) X Premium subscription Uses Grok model
OpenRouter OpenRouter account OAuth creates an API Key and charges against the balance

In Claude Pro/Max subscription authentication mode, Pi Agent uses extra usage, billed by token, which doesn't count against your Claude plan quota. You can check it under the Claude usage settings .

After login completes, credentials are stored in ~/.pi/agent/auth.json and will be used automatically on subsequent Pi Agent starts.

To log out, use:



/logout


Method 2: API Key environment variable

If you have an API Key, you can set an environment variable before starting Pi Agent:



# Set Anthropic API Key

export ANTHROPIC_API_KEY=sk-ant-api03-your-key-here



# Start Pi Agent

pi

Pi Agent supports the following API Key environment variables for major providers:

Provider Environment Variables auth.json key name
Anthropic ANTHROPIC_API_KEY anthropic
OpenAI OPENAI_API_KEY openai
Google Gemini GEMINI_API_KEY google
DeepSeek DEEPSEEK_API_KEY deepseek
Groq GROQ_API_KEY groq
Mistral MISTRAL_API_KEY mistral
xAI XAI_API_KEY xai
OpenRouter OPENROUTER_API_KEY openrouter

Configure the DeepSeek provider

Pi supports custom providers via models.json; the configuration file is located at:

First obtain an API Key from the DeepSeek open platform:https://platform.deepseek.com/api_keys。

{

  "providers": {

    "deepseek": {

      "baseUrl": "https://api.deepseek.com",

      "api": "openai-completions",

      "apiKey": "$DEEPSEEK_API_KEY",

      "models": [

        {

          "id": "deepseek-v4-pro",

          "name": "DeepSeek V4 Pro",

          "contextWindow": 1000000,

          "maxTokens": 384000,

          "input": ["text"],

          "reasoning": true,

          "cost": {

            "input": 1.74,

            "output": 3.48,

            "cacheRead": 0.145,

            "cacheWrite": 0

          },

          "compat": {

            "requiresReasoningContentOnAssistantMessages": true,

            "thinkingFormat": "deepseek",

            "reasoningEffortMap": {

              "minimal": "high",

              "low": "high",

              "medium": "high",

              "high": "high",

              "xhigh": "max"

            }

          }

        },

        {

          "id": "deepseek-v4-flash",

          "name": "DeepSeek V4 Flash",

          "contextWindow": 1000000,

          "maxTokens": 384000,

          "input": ["text"],

          "reasoning": true,

          "cost": {

            "input": 0.14,

            "output": 0.28,

            "cacheRead": 0.028,

            "cacheWrite": 0

          },

          "compat": {

            "requiresReasoningContentOnAssistantMessages": true,

            "thinkingFormat": "deepseek",

            "reasoningEffortMap": {

              "minimal": "high",

              "low": "high",

              "medium": "high",

              "high": "high",

              "xhigh": "max"

            }

          }

        }

      ]

    }

  }

}

Set environment variables:

Linux / Mac users:



export DEEPSEEK_API_KEY="<你的 DeepSeek API Key>"

Windows users:



$env:DEEPSEEK_API_KEY="<你的 DeepSeek API Key>"

Enter the project directory and run the pi command:



cd /path/to/my-project

pi

If you don't want to set the environment variable manually each time, you can write the export command into a shell config file (such as ~/.zshrc or ~/.bashrc) to make it permanent.


Method 3: API Key credential file

You can also use the /login command to select an API Key provider for storing credentials.

The storage location is ~/.pi/agent/auth.json, in the following format:



{

  "anthropic": { "type": "api_key", "key": "sk-ant-api03-your-key" },

  "openai": { "type": "api_key", "key": "sk-your-openai-key" },

  "deepseek": { "type": "api_key", "key": "sk-your-deepseek-key" }

}

The credential file is automatically set to 0600 permissions (readable and writable only by the user), providing basic security.


Credential resolution order

When Pi Agent needs to obtain an API Key, it looks in the following order of priority:

  1. CLI argument --api-key (highest priority)
  2. Credentials in the auth.json file
  3. Environment Variables
  4. Key of a custom provider in models.json

Pi Agent credential resolution priority chain

In other words, if you set both an environment variable and auth.json, the value in auth.json takes precedence.


Advanced auth.json features

auth.json not only supports storing plaintext Keys, but also the following advanced usages:

Obtaining a Key from the command line

Use the ! prefix to execute a command to obtain the Key, suitable for reading from a password manager:



{

  "anthropic": {

    "type": "api_key",

    "key": "!security find-generic-password -ws 'anthropic-api-key'"

  },

  "openai": {

    "type": "api_key",

    "key": "!op read 'op://vault/item/credential'"

  }

}

The example above shows how to obtain a Key from macOS Keychain and 1Password CLI.

Environment variable interpolation

Use the $ prefix to reference environment variables:



{

  "anthropic": {

    "type": "api_key",

    "key": "$MY_ANTHROPIC_KEY"

  }

}

Switching models

Once authentication is configured, you can switch the AI model you use at any time:

In interactive mode, use the /model command or press Ctrl+L to open the model selector.

Use Ctrl+P / Shift+Ctrl+P to cycle through the configured models.

Specify a model from the command line:



# Specify provider and model

$ pi --provider anthropic --model claude-sonnet-4-20250514



# Using provider/model format

$ pi --model openai/gpt-4o "帮我重构这段代码"



# Specify inference level (thinking level)

$ pi --model sonnet:high "解决这个复杂问题"

The Thinking Level controls how deeply the model thinks before answering. Levels range from off to max; deeper reasoning may produce higher-quality answers but consumes more tokens.

Reasoning level Description Applicable scenarios
off Do not show the reasoning process Simple Q&A, code formatting
minimal Minimal reasoning Basic coding tasks
low Low reasoning General programming problems
medium Medium reasoning (default recommended) Daily development tasks
high High reasoning Complex architecture design, debugging
xhigh Extra-high reasoning Difficult algorithm problems
max Maximum reasoning Extremely complex logic analysis

The editor's border color changes with the reasoning level, letting you see the current reasoning depth intuitively.