Skip to main content

Python SDK for zag — a unified CLI for AI coding agents

Project description

Zag Python Binding

Python binding for zag — a unified CLI for AI coding agents.

Prerequisites

  • Python 3.10+
  • The zag CLI binary installed and on your PATH (or set via ZAG_BIN env var)

Installation

pip install zag-agent

For development from source:

cd bindings/python
pip install -e .

Quick start

from zag import ZagBuilder

output = await ZagBuilder() \
    .provider("claude") \
    .model("sonnet") \
    .auto_approve() \
    .exec("write a hello world program")

print(output.result)

Streaming

from zag import ZagBuilder

async for event in await ZagBuilder().provider("claude").stream("analyze code"):
    print(event.type, event)

Builder methods

Method Description
.provider(name) Set provider: "claude", "codex", "gemini", "copilot", "ollama"
.model(name) Set model name or size alias ("small", "medium", "large")
.system_prompt(text) Set a system prompt
.root(path) Set the working directory
.auto_approve() Skip permission prompts
.add_dir(path) Add an additional directory (chainable)
.file(path) Attach a file to the prompt (chainable)
.env(key, value) Add an environment variable for the agent subprocess (chainable)
.json_mode() Request JSON output
.json_schema(schema) Validate output against a JSON schema (implies .json_mode())
.json_stream() Enable streaming NDJSON output
.worktree(name=None) Run in an isolated git worktree
.sandbox(name=None) Run in a Docker sandbox
.session_id(uuid) Use a specific session ID
.output_format(fmt) Set output format ("text", "json", "json-pretty", "stream-json")
.input_format(fmt) Set input format ("text", "stream-json" — Claude only)
.replay_user_messages() Re-emit user messages on stdout (Claude only)
.include_partial_messages() Include partial message chunks (Claude only)
.max_turns(n) Set the maximum number of agentic turns
.timeout(duration) Set a timeout duration (e.g., "30s", "5m", "1h"). Kills the agent if exceeded.
.mcp_config(config) MCP server config: JSON string or file path (Claude only)
.show_usage() Show token usage statistics (JSON output mode)
.size(size) Set Ollama model parameter size (e.g., "2b", "9b", "35b")
.verbose() Enable verbose output
.quiet() Suppress non-essential output
.debug() Enable debug logging
.bin(path) Override the zag binary path

Terminal methods

Method Returns Description
.exec(prompt) AgentOutput Run non-interactively, return structured output
.stream(prompt) AsyncGenerator[Event] Stream NDJSON events
.exec_streaming(prompt) StreamingSession Bidirectional streaming (Claude only)
.run(prompt=None) None Start an interactive session (inherits stdio)
.resume(session_id) None Resume a previous session by ID
.continue_last() None Resume the most recent session

Version checking

The SDK automatically checks the installed zag CLI version before running commands. If you use a builder method that requires a newer CLI version than what's installed, a clear error is raised:

env() requires zag CLI >= 0.6.0, but the installed version is 0.5.0.
Please update the zag binary.

The version is detected once (by running zag --version) and cached for the lifetime of the process.

Method Minimum CLI version
.env() 0.6.0
.mcp_config() 0.6.0

All other methods are available since the initial release (0.2.3).

Discovery

Standalone async functions for discovering available providers, models, and capabilities:

from zag import list_providers, get_capability, get_all_capabilities, resolve_model

providers = await list_providers()
cap = await get_capability("claude")
all_caps = await get_all_capabilities()
resolved = await resolve_model("claude", "small")  # ResolvedModel(input="small", resolved="haiku", is_alias=True)
Function Description
list_providers(bin=None) List available provider names
get_capability(provider, bin=None) Get capabilities for a provider
get_all_capabilities(bin=None) Get capabilities for all providers
resolve_model(provider, model, bin=None) Resolve a model alias

How it works

The SDK spawns the zag CLI as a subprocess (zag exec -o json or -o stream-json) and parses the JSON/NDJSON output into typed dataclasses. Zero external dependencies — only the Python standard library.

Testing

pip install pytest pytest-asyncio
pytest

See also

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zag_agent-0.8.2.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zag_agent-0.8.2-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file zag_agent-0.8.2.tar.gz.

File metadata

  • Download URL: zag_agent-0.8.2.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for zag_agent-0.8.2.tar.gz
Algorithm Hash digest
SHA256 073f6f64487b56e92eeaca3c840c68c5d77a8ec5694df5cdf9f8abd33fda17d2
MD5 12e30d9783d1ba2198f9dee64e191a91
BLAKE2b-256 924650c59be6abd464fb5bad1e4577b280658c76d6fda4631e988cfb6cf4af0a

See more details on using hashes here.

File details

Details for the file zag_agent-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: zag_agent-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for zag_agent-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 af0d291449d698c3d012f2ec016947f6d17793315b3940d3cf881159d5e6fef5
MD5 fdaf06a76f4da48f79925733a963f33d
BLAKE2b-256 5a8a0a17b8859a3216b2f3ef2e3018b85fc6673574a1bbf857e494cf1c893af8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page