Skip to content

API reference

Imports

A first program's names come from the root; everything else comes from the layer that owns it, so an import path says which part of the library a name belongs to.

from deepharness import Agent, Graph, Message, OpenAI, tool  # ~27 high-level names
from deepharness.agent import ContextPolicy, StepStarted
from deepharness.graph import concat, merge_dicts
from deepharness.providers import Caching, Fallback, Image, Text
from deepharness.sandbox import Bubblewrap
from deepharness.tools import Permissions, Workspace
from deepharness.errors import ProviderError
Module Holds
deepharness Agent, AgentState, Budget, Message, Toolbox, Ctx, tool, Finished, TextDelta, Graph, Executor, DeepHarnessError, and all 15 providers
deepharness.agent the loop's own types — ContextPolicy, estimate_tokens, the progress events, StopReason, PendingHumanInput, ToolSpec, FINAL_TOOL
deepharness.graph NodeSpec, concat, merge_dicts
deepharness.providers LLM and the wire types, content blocks (Text, Image, Document, Thinking), ReasoningLevel, and the wrappers Caching/Fallback/RateLimited/Retrying/Wrapping
deepharness.tools Permissions, Rule, Workspace, MCPServer, Transport
deepharness.sandbox Sandbox, Bubblewrap
deepharness.errors every exception; only DeepHarnessError is re-exported at the root

The root is kept small on purpose: each name there is a promise about stability, and a flat namespace of everything stops being discoverable long before it is complete.

The public surface, importable from deepharness unless noted otherwise. This page covers signatures and behavior only — see the guide for narrative explanations and examples.

Agents

Agent

Agent(
    model: LLM | None = None,
    *,
    tools: Iterable[Callable] | Toolbox = (),
    system: str | None = None,
    name: str = "agent",
    budget: Budget | None = None,
    context: ContextPolicy | None = None,
    permissions: Permissions | None = None,
    output: type | None = None,
    on_event: EventHook | None = None,
)

Runs a think/act loop against model: request a completion, dispatch any requested tool calls, repeat until the model stops calling tools or the budget's step limit is reached.

Member Signature Description
arun async def arun(state: Any = None, *, deps: Any = None) -> AgentState Async run. Tool calls in the same turn dispatch concurrently.
run def run(state: Any = None, *, deps: Any = None) -> AgentState Sync run. Raises if a registered tool is async def.
astream async def astream(state=None, *, deps=None) -> AsyncIterator[str] Text deltas as they arrive; tools still dispatch.
astream_events async def astream_events(...) -> AsyncIterator[AgentEvent] Text deltas, progress events and a final Finished(state).
stream / stream_events sync counterparts Same, outside an event loop.
total_usage TokenUsage Cumulative token usage across every call made by this agent instance. Read-only.
budget Budget The run's limits; defaults to Budget() when none is passed. Read-only.
context ContextPolicy What the transcript may cost; defaults to ContextPolicy(). Read-only.
permissions Permissions \| None Per-call policy; None leaves each call to the tool's own requires_approval. Read-only.
tools Toolbox Always a Toolbox — an iterable passed as tools= is wrapped in one. Read-only.
output type \| None A dataclass; when set, state.output is a validated instance of it.
on_event Callable[[AgentEvent], Awaitable[None] \| None] \| None Called with every event of every run, before the caller sees it, so arun()/run() can be observed. May be async under arun(); a sync run() raises ConfigurationError for an async one. Its exceptions propagate.

AgentState

AgentState(
    messages: list[dict] = [],
    output: Any = None,
    usage: TokenUsage = TokenUsage(0, 0, 0),
    stop_reason: StopReason | None = None,
    paused: list[PendingHumanInput] = [],
)

What a run consumed and produced. stop_reason is one of "answer", "step_budget", "paused", "token_budget" or "truncated". answered is True only when stop_reason == "answer". AgentState.of(value) builds one from a prompt string, a list of messages, a dict of known fields, or an existing state; an unknown dict key raises ConfigurationError. to_dict()/from_dict(data) round-trip the whole state as JSON-able data, including any call paused on an approval, so a run can be stored wherever the caller likes and resumed in another process. Structured output is stored as plain data and returns as a dict.

approve(call_id=None) / reject(call_id=None) rule on calls waiting in paused, returning the state so a resume reads as await agent.arun(state.approve()). Both raise ConfigurationError when nothing matches.

PendingHumanInput

PendingHumanInput(
    call_id: str | None,
    name: str,
    question: str = "",
    arguments: dict | None = None,
    approved: bool | None = None,
)

One paused call. needs_approval is True when it came from a requires_approval tool — those have not run yet and carry the arguments to run with, and question is empty: how to put the call to a person is yours to word. Otherwise the pause came from a tool raising HumanInputRequired, question is what it asked, and the human's answer becomes that call's result.

Budget

Budget(steps: int = 10, tokens: int | None = None)

Frozen dataclass bounding one run. steps caps think/act turns — spending them stops the run with a "step_budget" stop reason rather than raising, since the run is truncated but already paid for. tokens caps cumulative usage and raises TokenBudgetExceeded when crossed. Budget(steps=1) makes an agent single-shot: one model call, and no turn to react to a tool result. Non-positive values raise ConfigurationError.

An Agent's state argument is anything AgentState.of() accepts — a prompt, a list of messages, or a state to resume — and the result is an AgentState. Without a model, run/arun are a no-op passthrough — useful as a placeholder while wiring a graph.

ContextPolicy

ContextPolicy(
    max_tokens: int | None = None,
    tool_result_chars: int | None = 8000,
    keep_last: int = 4,
)

Frozen dataclass bounding what a run sends. tool_result_chars truncates each tool result as it is recorded, eliding the middle. max_tokens prunes the transcript the model is sent — oldest turns first, with a note in their place — while state.messages keeps every message; it is None (no pruning) by default. Pruning never drops the leading system prompt, never orphans a tool result, and always keeps the last keep_last messages, so a transcript whose tail alone exceeds the budget is sent over it. Non-positive values raise ConfigurationError.

Member Signature Description
prune def prune(messages: list[dict]) -> list[dict] The transcript as it should be sent; returns the list unchanged when it fits. Override to prune differently.

estimate_tokens

estimate_tokens(messages: list[dict]) -> int

Approximate token cost of a transcript, at roughly four characters per token. A heuristic, not a tokenizer — a real count would mean a per-vendor dependency.

AgentEvent

AgentEvent = (
    TextDelta
    | ThinkingDelta
    | StepStarted
    | StepFinished
    | ToolStarted
    | ToolFinished
    | Finished
)

What astream_events()/stream_events() emit.

Event Fields Emitted
StepStarted step: int Before each model call; 1-based, capped by Budget.steps.
StepFinished step: int, usage: TokenUsage \| None, duration: float Once the model's turn ends, before its tools run. duration is seconds and excluded from equality.
ToolStarted name: str, arguments: dict, call_id: str \| None Before a tool runs, with the arguments the model sent.
ToolFinished name: str, result: str, failed: bool, call_id: str \| None, duration: float After a tool returns or raises, as each ends. result is the (truncated) text the model will read. Not emitted for a tool that asked a human — it has no result yet.
TextDelta text: str As the model's prose arrives.
ThinkingDelta text: str As the model's reasoning arrives, kept apart from the answer.
Finished state: AgentState Once, last, carrying the run's result.

TokenBudgetExceeded

TokenBudgetExceeded(agent_name: str, usage: TokenUsage, budget: int, state: dict | None = None)

state carries the run as it stood when the budget was crossed.

Raised by Agent the moment cumulative total_usage crosses Budget.tokens, checked right after a model response — before any further tool dispatch or model call.

Messages & sessions

Message

A dataclass with role, content, name, tool_calls and tool_call_id; to_dict() gives the wire dict. Anywhere a message is accepted, a plain dict works too.

Constructor to_dict() gives
Message.system(content: str) {"role": "system", "content": ...}
Message.human(content: Content \| Block) {"role": "user", "content": ...}
Message.ai(content: Content \| Block, *, tool_calls: list[dict] \| None = None) {"role": "assistant", "content": ...}
Message.tool(content: str, *, name: str, call_id: str \| None = None) {"role": "tool", "content": ..., "name": ..., "tool_call_id": ...}

Tools

tool

@tool
@tool(
    name: str | None = None,
    description: str | None = None,
    requires_approval: bool = False,
)

Decorates a function so it can be registered as a callable tool. Builds a JSON schema from the function's signature (parameter types, required-ness) and docstring (summary plus Args:/:param: descriptions). Handles containers, Literal, Enum and unions — see Tools. Works on both sync and async functions. requires_approval=True makes the agent pause before every call to it and run it only once approved.

Ctx

Ctx(state: Any = None, deps: Any = None)

Injected into any tool parameter annotated Ctx, and hidden from that tool's schema. deps is whatever the run was given; state is the run's AgentState.

Toolbox

Toolbox(tools: Iterable[Callable] = (), *, approve: Approver | None = None)

approve(name, arguments) is called before each requires_approval call runs, wherever the toolbox is called from; True runs it and False raises ToolDenied. It may be async.

Method Signature Description
register def register(func: Callable) -> Callable Registers a function as a tool.
get def get(name: str) -> ToolSpec Looks up a registered tool by name.
schemas def schemas() -> list[dict] Returns tool schemas, ready to pass to a provider.
call async def call(name: str, arguments: dict \| None = None, *, ctx: Ctx \| None = None, ask: bool \| None = None) -> Any Invokes a tool by name, awaiting it if async.
call_sync def call_sync(name: str, arguments: dict \| None = None, *, ctx: Ctx \| None = None, ask: bool \| None = None) -> Any Invokes a tool synchronously; raises if it's async.

Workspace

Workspace(root: str | Path = ".")

The one directory a tool may touch. resolve(path) returns an absolute path inside the root or raises OutsideWorkspace; resolution is symlink-aware, so a link inside the root pointing out of it is refused. relative(path) renders a resolved path as the model should see it. A root that is not an existing directory raises ConfigurationError.

Sandbox

Bubblewrap(
    workspace=".",
    *,
    writable=True,
    network=False,
    read_only=(),
    max_output=1_000_000,
    max_memory_mb=None,
    max_processes=None,
)

From deepharness.sandbox. Where a command runs: the boundary a tool you write calls instead of subprocess. Use as with or async with; leaving the block discards everything outside the workspace, and run outside it raises ConfigurationError. Subclass Sandbox for a new backend.

Member Signature Description
workspace Workspace The directory mounted into the sandbox at its own path.
run def run(command, timeout) -> CompletedProcess[str] Run one command; raises subprocess.TimeoutExpired once it is killed. Each stream keeps at most max_output bytes. Abstract.
start / stop def start() -> None Acquire and release what the sandbox holds; called by with. No-ops by default.

MCPServer / MCPTool

MCPServer.stdio(command: str | Iterable[str], *, env=None, requires_approval=True)
MCPServer.http(url: str, *, headers=None, client=None, requires_approval=True)
MCPServer(transport: Transport, *, requires_approval: bool = True)

A Model Context Protocol server's tools as callables. The handshake runs on first use; async with closes the transport.

Member Signature Description
tools async def tools() -> list[Callable] The server's tools, ready for Agent(tools=...). Async, so use arun().
list_tools async def list_tools() -> list[MCPTool] The same tools as data: name, description, input_schema, read_only.
call async def call(name, arguments) -> str Run one tool; raises MCPError if the server reports failure.
connect / aclose async def ...() -> None Handshake and shutdown; both are idempotent.

A tool the server did not mark read-only is gated with requires_approval=True. A protocol-level failure raises MCPError.

Transport is the seam — request(method, params), notify(method, params), aclose() — with two implementations: StdioTransport (a subprocess speaking newline-delimited JSON, reading past any notifications to its own reply) and HTTPTransport (streamable HTTP, JSON or SSE replies, carrying any Mcp-Session-Id). The deprecated 2024-11-05 HTTP+SSE transport and OAuth are not implemented, and only MCP's tools are — not resources, prompts or sampling. Implement Transport yourself and pass it to MCPServer(transport) to cover a server these two do not reach.

Permissions / Rule

Permissions(
    *,
    allow: Iterable[RuleLike] = (),
    ask: Iterable[RuleLike] = (),
    deny: Iterable[RuleLike] = (),
)
Rule(tool: str, arguments: dict[str, str] | None = None)

RuleLike = str | Rule | Callable[..., Any]   # a name or pattern, a Rule, or the tool itself

Decides per call what may run. decide(name, arguments) -> "allow" | "ask" | "deny" | None, where None means no rule applied and the tool's own requires_approval stands. deny beats allow beats ask.

A rule may be written two ways: the decorated tool itself (read for the name it is registered under), or a string name or fnmatch pattern. Rule narrows by argument patterns, also fnmatch; an argument a rule mentions but the call omits does not match. Rule.is_pattern says whether a rule names a tool or a shape.

Permissions.rules is every rule; Permissions.gates is the deny and ask rules — the ones whose silence would be unsafe. Agent refuses at construction when a gate names a tool it has not registered, since a misspelled deny rule matches nothing and allows what it was written to stop. Allow rules and pattern rules are not checked.

Graphs & execution

Graph

Graph(state_type: type)
Method Signature Description
add def add(*, name: str \| None = None, start: bool = False, end: bool = False) Decorator that registers a function as a node.
connect def connect(source: Callable \| str, target: Callable \| str, *, condition: Callable[[Any], bool] \| None = None, loop: bool = False) Declares an edge, optionally conditional. loop=True marks a back-edge, re-running the loop head and everything downstream of it.
build def build(*, on_event: GraphEventHook \| None = None) -> Executor on_event is called with every GraphEvent of every run, so run() can be observed; it may be async. Validates the graph (has a start node, fully reachable, no cycle whose back-edge is unmarked) and returns an Executor.

Executor

async def run(state: Any = None, *, max_steps: int = 50) -> Any
async def astream_events(state: Any = None, *, max_steps: int = 50) -> AsyncIterator[GraphEvent]
def diagram() -> str

Omitting state builds one from the type the Graph was declared with, so a state whose fields all have defaults needs no argument. Runs the graph wave by wave: each wave's ready nodes execute concurrently, results merge back into the state field-by-field, and the next wave is whichever nodes now have all predecessors satisfied. A field written by two or more concurrent branches is combined by its reducer, or raises ConcurrentUpdateError if it declares none. Taking a loop=True edge re-runs the loop head and everything downstream of it; exceeding max_steps raises StepLimitExceeded. Returned by Graph.build() — not constructed directly.

astream_events() runs the graph the same way, yielding NodeStarted(name) for each node in a wave, then NodeFinished(name, duration) as each one ends, and finally GraphFinished(state). run() is that stream drained down to the final state. A node that raises cancels the others in its wave. The events are importable from deepharness.graph.

diagram() returns a box-drawing picture of the graph as text — layers top to bottom by wave, ═ for start and end nodes, ▽ for a conditional edge, and back-edges routed up the right margin. It returns the string rather than printing it. See Seeing the shape.

concat / merge_dicts

concat(base: list, values: list[list]) -> list
merge_dicts(base: dict, values: list[dict]) -> dict

Built-in reducers for merging concurrent writes to one field. Declare one with field(metadata={"reducer": concat}).

ExecutionError

ExecutionError(node_name: str, original: Exception)

Raised by Executor.run when a node function raises; wraps the original exception with the name of the node that failed.

Providers

Every provider below implements the same interface:

Method Returns
await agenerate(messages: list[dict], *, tools: list[dict] \| None = None) CompletionResponse
generate(messages: list[dict], *, tools: list[dict] \| None = None) CompletionResponse
async for event in astream_events(messages, *, tools=None) TextDelta/ThinkingDelta, then Completed(response)
for event in stream_events(messages, *, tools=None) TextDelta/ThinkingDelta, then Completed(response)
async for chunk in astream(messages, *, tools=None) text deltas (filters the above)
for chunk in stream(messages, *, tools=None) text deltas

A streaming turn ends with Completed, carrying the assembled CompletionResponse — tool calls included, reassembled from however the vendor fragmented them. agenerate/generate are the required pair — implement those two and a custom provider works everywhere, transport regardless. The streaming pair is optional: the base class raises NotImplementedError naming the provider, so a backend that cannot stream needs no stub. Vendors that speak REST share their request sequence through RestCompletions rather than by inheriting it (see providers/rest.py).

TokenUsage

TokenUsage(
    prompt_tokens: int,
    completion_tokens: int,
    total_tokens: int,
    cached_tokens: int = 0,
    cache_write_tokens: int = 0,
)

cached_tokens is the part of prompt_tokens the vendor served from its prompt cache; cache_write_tokens is what it charged to put a prefix there. Both are inside prompt_tokens, reported rather than deducted — a cached token still occupies the context window, so Budget(tokens=…) keeps counting the full figure. Read from prompt_tokens_details.cached_tokens (OpenAI, DeepSeek, llama.cpp), cache_read_input_tokens / cache_creation_input_tokens (Anthropic) and cachedContentTokenCount (Gemini); zero when a vendor does not report them. + sums every field, which is how agent.total_usage accumulates.

Content blocks

Text(text: str)
Image(data: str | None = None, media_type: str = "image/png", url: str | None = None)
Document(data: str, media_type: str = "application/pdf", name: str | None = None)
Thinking(text: str, signature: str | None = None)

Block = Text | Image | Document | Thinking
Content = str | list[Block]

What a message's content may be. Image.from_path(path) / Document.from_path(path) encode a local file and infer its media type; Image.from_url(url) passes a URL through. An Image with both data and url, or neither, raises ConfigurationError, as does a suffix no media type is known for. Providers render blocks into their own wire shape; Gemini rejects an image URL it cannot send. Text-only content is still sent — and saved — as a plain string.

Thinking.signature is Anthropic's attestation, replayed unmodified with the turn because Anthropic requires it back after a tool call; an unsigned thinking block is left out rather than sent.

Response types

ToolCall(name: str, arguments: dict, id: str | None = None)
CompletionResponse(
    content: str = "",
    tool_calls: list[ToolCall] = [],
    usage: TokenUsage | None = None,
    finish_reason: str = "stop",
    blocks: list[Block] = [],
)

CompletionResponse.thinking is the model's reasoning, if it returned any.

Direct providers

Class Signature
Anthropic Anthropic(model: str, api_key: str \| None = None, max_tokens: int = 4096, *, reasoning_effort=None, cache_prompt: bool = False, client=None, sync_client=None, max_concurrency: int \| None = None)
OpenAI OpenAI(model: str, api_key: str \| None = None, *, base_url: str \| None = None, temperature: float \| None = None, reasoning_effort=None, stream_usage: bool = True, client=None, sync_client=None, max_concurrency: int \| None = None)
Gemini Gemini(model: str, api_key: str \| None = None, *, reasoning_effort=None, client=None, sync_client=None, max_concurrency: int \| None = None)

api_key falls back to the vendor's standard environment variable when omitted: OPENAI_API_KEY, ANTHROPIC_API_KEY, and GEMINI_API_KEY (or GOOGLE_API_KEY).

client=/sync_client= inject your own httpx clients — a mock transport in tests.

max_concurrency caps that provider's in-flight requests, held across retries and for a stream's whole body — a fan-out of agents otherwise opens one connection per branch. cache_prompt=True (Anthropic) puts a cache breakpoint on the system prompt and the last tool definition, the part of a harness request that repeats every turn.

Each provider holds an HTTP connection pool. Call await model.aclose() — or model.close() from synchronous code — when you are done with one.

Wrapping a provider

Fallback(primary: LLM, *others: LLM, on: tuple[type[BaseException], ...] = (ProviderError,))
Caching(inner: LLM, *, maxsize: int = 256, ttl: float | None = None)
RateLimited(inner: LLM, *, rps: float, burst: int | None = None)
Retrying(inner: LLM, *, attempts: int = 2, backoff: float = 0.5)

Each is itself an LLM, so one wrapper covers generate, agenerate and both streaming paths, and they compose: Caching(RateLimited(Fallback(a, b), rps=2)). inner is the next layer down (models on Fallback), and aclose()/close() pass down.

Wrapper Behaviour
Fallback Tries each provider in order, catching only on — ProviderError by default, so a bug in your own code is not mistaken for a flaky vendor. All failing raises one ProviderError with the last as __cause__. A stream failing before its first event falls back; one failing partway raises.
Caching In-process LRU keyed on messages and tool schemas, with optional ttl. Hits are copied on the way out; a streamed hit is replayed in block order then Completed. Exposes hits, misses, clear(). No single-flight: concurrent identical calls all miss. Only sound in front of a deterministic setup.
RateLimited Token bucket at rps, capacity burst (default max(1, int(rps))). A caller reserves its slot then waits its own turn. Guarded by a threading.Lock, so one instance works across event loops.
Retrying Re-asks when a turn has no text and no tool calls. Transport-level retries already happen in HTTPClient. A stream is retried only while nothing has been emitted.

Non-positive limits raise ConfigurationError, as does a Fallback with nothing to fall back to. Wrapping is the shared base if you write your own: subclass it and override only the methods you change.

OpenAI-compatible gateways

All of these share OpenAI's constructor shape and read their own API key from the environment automatically:

Groq, Together, DeepSeek, Mistral, XAI, OpenRouter, Fireworks, Cerebras.

Local servers — no API key required: Ollama, VLLM, LMStudio, LlamaCpp.

For any other OpenAI-compatible endpoint, construct OpenAI directly with an explicit base_url and api_key.