Two distinct ways to use DeepSeek from Python:
model_api.py— calldeepseek-v4-flash-freedirectly via OpenCode Zen (needs API key)agent_sdk.py— drive opencode's built-in agent (file editing, shell, etc.) (no API key needed)
Call the underlying LLM directly via the OpenAI-compatible endpoint.
- Python 3.8+
- An OpenCode Zen API key — go to https://opencode.ai/zen to generate one
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Then edit .env and set OPENCODE_ZEN_API_KEY=<your-key>
python model_api.pyUses the OpenAI SDK pointed at https://opencode.ai/zen/v1 with model deepseek-v4-flash-free:
from openai import OpenAI
client = OpenAI(
api_key="<your-key>",
base_url="https://opencode.ai/zen/v1",
)
resp = client.chat.completions.create(
model="deepseek-v4-flash-free",
messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)curl -H "Authorization: Bearer $OPENCODE_ZEN_API_KEY" \
https://opencode.ai/zen/v1/modelsDrive opencode's full agent (can read/write files, run shell commands, etc.) from your own scripts.
- opencode CLI installed and configured
- opencode server running
pip install --pre opencode-aiTerminal 1 — start the opencode server:
opencode serveTerminal 2 — run the agent script:
python agent_sdk.pyConnects to the opencode server at http://localhost:4096, creates a session, and sends a message:
from opencode_ai import Opencode
from opencode_ai.types import TextPartInputParam
client = Opencode(base_url="http://localhost:4096")
session = client.session.create()
result = client.session.chat(
session.id,
model_id="opencode/deepseek-v4-flash-free",
provider_id="opencode",
mode="build", # or "plan", "general", "explore", "coder"...
parts=[TextPartInputParam(type="text", text="Write a hello world Python script to hello.py")],
)By default the web UI or server may pick a different model. To use deepseek-v4-flash-free every time, create .opencode.json in the project root:
{
"model": "opencode/deepseek-v4-flash-free"
}Or set it globally in ~/.config/opencode/opencode.jsonc.