zag
A unified Rust library for driving AI coding agents — Claude, Codex, Gemini, Copilot, and Ollama.
Overview
zag provides a programmatic Rust API for spawning and interacting with AI coding agents. It wraps multiple provider CLIs behind a common Agent trait and exposes a fluent AgentBuilder for ergonomic use. Write your agent logic once and swap providers without changing code.
Quick start
[]
= "0.1"
= { = "1", = ["full"] }
use AgentBuilder;
async
Features
- Multi-provider support: Claude, Codex, Gemini, Copilot, and Ollama
- Model size aliases: Use
small,medium,largeacross any provider - JSON output with schema validation: Request structured output and validate against JSON Schema
- Streaming sessions: Bidirectional NDJSON communication (Claude)
- Custom progress handlers: Plug in your own progress reporting
- Session management: Track, resume, and search session logs
- Worktree and sandbox isolation: Run agents in isolated environments
Examples
JSON schema validation
use AgentBuilder;
let schema = json!;
let output = new
.provider
.json_schema
.exec
.await?;
Streaming (Claude only)
use AgentBuilder;
let mut session = new
.provider
.exec_streaming
.await?;
session.send_user_message.await?;
while let Some = session.next_event.await?
session.wait.await?;
Custom progress handler
use ProgressHandler;
;
new
.on_progress
.exec
.await?;
Worktree isolation
use AgentBuilder;
// Run the agent in an isolated git worktree
let output = new
.provider
.worktree
.auto_approve
.exec
.await?;
Limiting agentic turns
use AgentBuilder;
let output = new
.provider
.max_turns
.exec
.await?;
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 root directory for the agent |
.auto_approve(bool) |
Skip permission prompts |
.add_dir(path) |
Add an additional directory (chainable) |
.json() |
Request JSON output |
.json_schema(schema) |
Validate output against a JSON schema (implies .json()) |
.json_stream() |
Enable streaming NDJSON output |
.worktree(name) |
Run in an isolated git worktree |
.sandbox(name) |
Run inside a Docker sandbox |
.session_id(uuid) |
Pre-set a 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(bool) |
Re-emit user messages on stdout (Claude only) |
.include_partial_messages(bool) |
Include partial message chunks (Claude only) |
.max_turns(n) |
Maximum number of agentic turns |
.size(size) |
Ollama parameter size (e.g., "2b", "9b", "35b") |
.show_usage(bool) |
Show token usage statistics |
.verbose(bool) |
Enable verbose output |
.quiet(bool) |
Suppress non-essential output |
.on_progress(handler) |
Set a custom progress handler |
Terminal methods
| Method | Returns | Description |
|---|---|---|
.exec(prompt) |
Result<AgentOutput> |
Run non-interactively, return structured output |
.exec_streaming(prompt) |
Result<StreamingSession> |
Bidirectional streaming (Claude only) |
.run(prompt) |
Result<()> |
Start an interactive session (inherits stdio) |
.resume(session_id) |
Result<()> |
Resume a previous session by ID |
.continue_last() |
Result<()> |
Resume the most recent session |
CLI
The companion binary crate zag-cli provides a full CLI:
See the repository for full documentation.
For complete projects using this API, see the examples directory — especially cv-review which demonstrates AgentBuilder, JSON schema validation, and parallel agent invocations.
See also
- Root README — Full CLI documentation
- zag-orch — Orchestration primitives (spawn, wait, collect, pipe)
- Language bindings — TypeScript, Python, and C# SDKs
- Examples — Complete projects demonstrating zag usage