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
- Visit https://platform.deepseek.com/
- Register and log in
- Go to "API Keys" page
- Click "Create API Key"
- 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
- DeepSeek is the recommended LLM provider for Pi Agent with excellent cost-effectiveness
- Get API key and configure via config file or environment variables
- deepseek-chat for daily use, deepseek-reasoner for complex reasoning
- Optimize with temperature, max_tokens, and tool_choice parameters
Exercises
- Basic (Difficulty: ⭐): Register a DeepSeek account, get an API key, configure Pi Agent, and send a message.
- Intermediate (Difficulty: ⭐⭐): Compare deepseek-chat and deepseek-reasoner on the same math problem.
- Advanced (Difficulty: ⭐⭐⭐): Build a cost monitoring tool that logs token usage and expenses per API call.