Pi Agent: DeepSeek Configuration

Last updated: 2026-08-31

DeepSeek is Pi Agent's most recommended provider — cheap, capable, excellent with Chinese. This lesson covers the complete DeepSeek configuration.


1. Why DeepSeek

Advantage Description
High cost-effectiveness Input ¥1/million tokens, Output ¥2/million tokens
Excellent Chinese Native Chinese training, superior understanding and generation
Function Calling Supports tool calling
Long context 64K context window
Fast response Low first-token latency

2. Getting an API Key

  1. Visit https://platform.deepseek.com/
  2. Register and log in
  3. Go to "API Keys" page
  4. Click "Create API Key"
  5. Copy the key (format: sk-xxxxxxxxxxxxxxxx)

Note: The key is shown only once. Save it securely. If compromised, delete it immediately and create a new one.


3. Basic Configuration

(1) Minimal Configuration

YAML
providers:
  deepseek:
    api_key: "sk-xxxxxxxx"

(2) Full Configuration

YAML
providers:
  deepseek:
    api_key: "sk-xxxxxxxx"
    base_url: "https://api.deepseek.com"
    model: "deepseek-chat"
    temperature: 0.7
    max_tokens: 4096
    top_p: 0.95
    timeout: 60

default_provider: deepseek

(3) Environment Variables

BASH
export PI_DEEPSEEK_API_KEY="sk-xxxxxxxx"
export PI_DEEPSEEK_BASE_URL="https://api.deepseek.com"

4. Model Selection

Model Use Case Context Price (per million tokens)
deepseek-chat General conversation 64K Input ¥1 / Output ¥2
deepseek-reasoner Complex reasoning 64K Input ¥4 / Output ¥16

Example 1: Comparing Models (Difficulty: ⭐)

PYTHON
from pi_agent import Agent

chat_agent = Agent(name="quick", provider="deepseek", model="deepseek-chat")
reasoner_agent = Agent(name="thinker", provider="deepseek", model="deepseek-reasoner")

print(chat_agent.chat("1+1=?"))
print(reasoner_agent.chat("Prove that the square root of 2 is irrational"))

5. Parameter Tuning

(1) temperature

Controls output randomness; lower values are more deterministic:

PYTHON
code_agent = Agent(model="deepseek-chat", temperature=0.1)     # Code: precise
creative_agent = Agent(model="deepseek-chat", temperature=1.0) # Creative: diverse

(2) max_tokens

Controls maximum response length:

PYTHON
brief_agent = Agent(model="deepseek-chat", max_tokens=256)     # Short replies
essay_agent = Agent(model="deepseek-chat", max_tokens=8192)    # Long essays

(3) Tool Calling Configuration

PYTHON
agent = Agent(
    provider="deepseek",
    model="deepseek-chat",
    tools=["search", "calculator"],
    tool_choice="auto"  # auto | required | none
)
tool_choice Behavior
auto Agent decides whether to call tools
required Must call a tool
none No tool calls allowed

6. Cost Optimization

Alice processes thousands of requests daily. She uses these strategies:

PYTHON
agent = Agent(
    provider="deepseek",
    model="deepseek-chat",
    max_tokens=2048,
    temperature=0.3,
    context_window=4096
)

Bob advises: "Use deepseek-chat for simple questions, deepseek-reasoner only for complex reasoning. Chat handles 90% of scenarios."


FAQ

Q DeepSeek API returns 401 error?
A The API key is invalid or expired. Check it on the platform — make sure there are no extra spaces or newlines.
Q Responses are slow?
A Check your network connection, reduce max_tokens, decrease context window size. Peak hours may have increased latency.
Q Is DeepSeek API compatible with OpenAI?
A Yes. DeepSeek's API format is OpenAI-compatible. You can set base_url to DeepSeek and use the OpenAI SDK. Pi Agent has this built in.

Summary


Exercises

  1. Basic (Difficulty: ⭐): Register a DeepSeek account, get an API key, configure Pi Agent, and send a message.
  2. Intermediate (Difficulty: ⭐⭐): Compare deepseek-chat and deepseek-reasoner on the same math problem.
  3. Advanced (Difficulty: ⭐⭐⭐): Build a cost monitoring tool that logs token usage and expenses per API call.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏