Python SDK for Opencode — the open source AI coding agent
Project description
Opencode Python SDK
Python SDK for Opencode — the open source AI coding agent.
pip install opencode-py
Quick start
One-shot (spawns server, asks, cleans up)
from opencode import opencode
answer = opencode("What is the capital of France?")
print(answer)
Context manager (recommended)
from opencode import Opencode
with Opencode() as ai:
answer = ai.ask("Explain dependency injection")
print(answer)
Streaming
with Opencode() as ai:
for chunk in ai.ask_stream("Write a Python function"):
print(chunk, end="")
Conversations
with Opencode() as ai:
session = ai.create_session()
msg1 = session.prompt("Suggest a project name")
print(f"AI: {msg1}")
msg2 = session.prompt("Now write a tagline for it")
print(f"AI: {msg2}")
Multi-turn (keep mode)
from opencode import opencode
# keep=True — server and session stay alive between calls
r1 = opencode("My name is Alice", keep=True)
r2 = opencode("What's my name?", keep=True) # remembers the conversation
r3 = opencode("That's all", keep=False) # keep=False closes the server
Auto-tools (agentic tool execution)
r = opencode("Create a file called hello.txt", auto_tools=True)
Available tools: bash, write, edit, read, glob, grep.
By default, bash asks for permission in the console, all others run without prompting.
Custom permissions via Session.ask():
from opencode import Opencode, ToolExecutor
with Opencode() as ai:
session = ai.create_session()
msg = session.ask(
"Write test.py with print('hello')",
tool_executor=ToolExecutor(permissions={"write": "allow"}),
)
Low-level API (any endpoint)
with Opencode() as ai:
content = ai.client.file_read("src/main.py")
diff = ai.client.vcs_diff("HEAD~3")
config = ai.client.config_get()
session = ai.client.session_create()
ai.client.v2_session_prompt(session.id, {"text": "Hello"})
All client methods return typed Pydantic models — IDE autocomplete, validation, .model_dump(), .model_dump_json().
Retry & error handling
from opencode import OpencodeClient, RateLimitError, InternalServerError
client = OpencodeClient(max_retries=3) # exponential backoff with jitter
try:
health = client.health()
print(health.version)
except RateLimitError:
print("too many requests — retried but failed")
except InternalServerError:
print("server error")
Debug logging
OPENCODE_LOG=debug python my_script.py
Shows all HTTP requests/responses with timing.
Web UI (zero dependencies)
python web/server.py
# → open http://127.0.0.1:3000
Built-in HTTP server + proxy to opencode serve — no extra dependencies.
Interactive dialog
python live.py
Multi-turn dialog with keep=True, server cleaned up on exit via atexit.
Configuration
with Opencode(
model="claude-sonnet-4-20250514",
directory="/path/to/project",
port=4096,
) as ai:
...
Async API
import asyncio
from opencode import AsyncOpendcode
async def main():
async with AsyncOpendcode() as ai:
answer = await ai.ask("Explain async/await in Python")
print(answer)
asyncio.run(main())
Async streaming
async with AsyncOpendcode() as ai:
async for chunk in ai.ask_stream("Write a poem"):
print(chunk, end="")
Async conversations
async with AsyncOpendcode() as ai:
session = await ai.create_session()
msg1 = await session.prompt("Suggest a project name")
msg2 = await session.prompt("Now write a tagline for it")
Async low-level client
from opencode import AsyncOpendcodeClient
async with AsyncOpendcodeClient() as client:
health = await client.health()
print(health.version) # typed Pydantic model
Development
# Install in editable mode
pip install -e ".[dev]"
# Run tests
pytest
# Build
python -m build --wheel
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencode_py-0.2.0.tar.gz.
File metadata
- Download URL: opencode_py-0.2.0.tar.gz
- Upload date:
- Size: 52.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1f7ae09a584dde60c31c7d66b333e23e3fb21cdaf4c6becc78d4a9bc0cf058a
|
|
| MD5 |
0b13eb66957db16cf297a3e07a494919
|
|
| BLAKE2b-256 |
80fede3fcffda0a302bd6367e47fc18e8f447ae0d39168fcea8819c2b3c09608
|
Provenance
The following attestation bundles were made for opencode_py-0.2.0.tar.gz:
Publisher:
publish.yml on skislyakow/opencode-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opencode_py-0.2.0.tar.gz -
Subject digest:
b1f7ae09a584dde60c31c7d66b333e23e3fb21cdaf4c6becc78d4a9bc0cf058a - Sigstore transparency entry: 2064024362
- Sigstore integration time:
-
Permalink:
skislyakow/opencode-py@ed8d40249032bfbe68723aa9aa11b7bb6afdff23 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/skislyakow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ed8d40249032bfbe68723aa9aa11b7bb6afdff23 -
Trigger Event:
push
-
Statement type:
File details
Details for the file opencode_py-0.2.0-py3-none-any.whl.
File metadata
- Download URL: opencode_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26f3f9db48a4cac033f15e085025d96f0cda800aa875f2cc8f7b02992a1c4e56
|
|
| MD5 |
1abc77885fe773b4c4bb2ee656358e0e
|
|
| BLAKE2b-256 |
5ea48797033ec053029dcfcb9582b8b15b3bdaf7fd6527bd12493a605c4ddd7d
|
Provenance
The following attestation bundles were made for opencode_py-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on skislyakow/opencode-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
opencode_py-0.2.0-py3-none-any.whl -
Subject digest:
26f3f9db48a4cac033f15e085025d96f0cda800aa875f2cc8f7b02992a1c4e56 - Sigstore transparency entry: 2064024372
- Sigstore integration time:
-
Permalink:
skislyakow/opencode-py@ed8d40249032bfbe68723aa9aa11b7bb6afdff23 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/skislyakow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ed8d40249032bfbe68723aa9aa11b7bb6afdff23 -
Trigger Event:
push
-
Statement type: