Skip to content
jimbakerPublic

About

Durable, replayable agentic workflows in Python: algebraic effects as composable generators, PEP 750 t-strings as typed prompt channels.

Topics

Resources

Stars

7 stars

Watchers

0 watching

Forks

Repository files navigation

Effective

Build agentic workflows from composable Python generators. A handler records each model call, Jev judgment, tool, and human approval, so a run is replayable and durable.

An agent loop is a state machine. A workflow writes it as a plain Python generator whose frames hold the state, and which yields an op for each effect: call a model, call a tool, wait for a human, append to the ledger, the run's append-only record. A handler interprets each op, so the same generator is recorded in a test, replayed from that recording, or run durably, where a crashed worker resumes by replaying the steps it already took.

The prompts are t-strings. A prompt is a t"..." whose interpolations are typed channels: inputs render in, and outputs declare the schema the response is parsed into. A Gated output runs a check over the response, which is a guardrail on the data boundary.

An agent loop is a state machine

A ReAct agent reasons, acts and observes, and something drives it: it calls the model, runs the tool, and sends the result back. Agent frameworks let you customize that machine with hooks on its edges: before a call, after a call, on a prompt, before stopping. Claude Code calls them PreToolUse, PostToolUse, UserPromptSubmit and Stop.

block-beta
    columns 5
    space:2 world["<b>World</b><br/>the model, tools, people"] prompt(["on a prompt"]) space
    space before(["before a call"]) space after(["after a call"]) space
    workflow["<b>Workflow</b><br/>a generator: its frames<br/>hold the state"] space handler["<b>Handler</b><br/>drives the loop,<br/>interprets ops"] space:2
    stopping(["before stopping"]) space:4
    space:5
    tape["<b>Tape</b><br/>one row per result; on replay it serves results first"]:5
    workflow -- "yield op ⇄ send" --- handler
    handler -- "call ⇄ result" --- world
    handler -- "append ⇄ replay" --- tape
    classDef hook fill:#fdebd9,stroke:#c2410c,color:#9a3412
    classDef hub stroke:#1d6fd6,stroke-width:2px
    classDef dashed stroke-dasharray:5 4
    class before,after,prompt,stopping hook
    class handler hub
    class tape dashed
Loading

The orange tags name those hooks: a call's two hooks flank the edge to the world, and "before stopping" sits by the workflow, whose return ends the run. The tape is how a run survives a crash, below.

Python already has a construct that holds a state machine's state for you: a generator. Each yield is an edge, and the suspended frame is the state.

def react(goal: str) -> Generator[Request, str]:
    observation = None
    while True:
        action = yield Reason(goal, observation)
        observation = yield Act(action)

Reason and Act are frozen dataclasses that describe a request. The loop performs no I/O, so whatever calls send decides what they mean: a test script, a live model, or the tape of an earlier run. examples/react_toy.py drives it with a script. In Effective, the code that calls send is a handler.

a state machine with hooks a generator
where the state lives fields you declare and keep current the frame's locals and the line it is paused on
a check before a call a hook function on an event the framework offers an expression at the yield, or a layer
a nested workflow the framework's subagent, if it has one; else a nested machine, forwarded by hand yield from another workflow, or spawn it as a child task
a new check a new hook function, or a new phase with its arm and fields a new line, which in-flight runs meet on replay
what a type checker proves a total transition table over a closed set of phases a total match over the closed set of ops; control flow only as far as the tests reach
durability checkpoint the state object replay the tape through the same code
changing code while runs are in flight load the stored state into the new code, migrating its shape if it changed the new code yields the recorded ops under their recorded names; an op it renames runs again
inspecting a paused run read the state object read the tape; the frame's locals are never stored

Effective's hooks are generators too. A layer sees each model call, tool call, sleep and ledger append on its way out and its result on the way back, and keeps whatever it needs between the two in its own frame:

@op_layer
def audit(op: WorkflowOp) -> Generator[WorkflowOp, Any, Any]:
    print("before a call:", describe(op))
    result = yield op
    print("after a call: ", describe(op), "->", result)
    return result

A layer runs again when a run replays: it sees each replayed op and its recorded result, so a side effect that must happen once belongs in an op. A layer that yields an AwaitEvent parks the run until a person answers. govern composes several checks into one gate that proceeds, parks or refuses. Retry belongs one level down, in retry_domain on the interpreter that interprets the op (MeteredInterpreter(..., domain_layers=[retry_domain()])): it retries inside the op's one checkpoint, where a re-yielded op would take a new checkpoint name and a crash would run it again.

A first workflow

git clone https://github.com/jimbaker/effective && cd effective
uv sync                                    # Python 3.14 and the development environment
uv run python examples/first_workflow.py   # a durable run, a resume after an outage, a guardrail

The workflow asks a model for a thermostat setpoint, checks it, and sets it:

def set_temperature(request_id: str, request: str, ceiling: float = 30) -> Effect[float | Repair]:
    celsius = Gated(float, lambda c: 10 <= c <= ceiling, "celsius outside the allowed range")
    prompt = t"The occupant said: {request}\nSet the thermostat to {celsius}"
    setpoint = render(prompt, output=Setpoint)
    response = yield from ask_llm("setpoint", setpoint.messages, dict)
    match setpoint.resolve(response):
        case Repair() as repair:
            return repair
        case Setpoint(celsius=target):
            yield from call_tool("thermostat", {"celsius": target}, str)
            ...

It runs as a task on the embedded SQLite engine, in a temporary file, and prints three lines; docs/first-workflow.md walks through it line by line:

line what it shows
run each op is a checkpointed step, and the keys printed are the checkpoints
resume the thermostat is offline on the first attempt; the retry replays the setpoint from the file and asks the model nothing more
guardrail the model responds with 45°C, the Gated check fails, and the workflow returns a Repair before it reaches the thermostat

resolve returns Repair(reason) when a check fails; the workflow decides what to do with it: return it, as here, or re-prompt with the reason.

Durability is a tape

A durable handler writes each result down before the workflow sees it. After a crash, a new worker runs the workflow from the top, and the tape serves every op it holds a result for:

sequenceDiagram
    participant W as Workflow
    participant H as Handler
    participant T as Tape
    participant M as Model
    W->>H: yield ask_llm("setpoint")
    H->>M: ask
    M-->>H: {"celsius": 22}
    H->>T: append the result
    H-->>W: send {"celsius": 22}
    Note over W,M: the worker crashes, and a new one runs the workflow from the top
    W->>H: yield ask_llm("setpoint")
    H->>T: read setpoint
    T-->>H: {"celsius": 22}
    H-->>W: send {"celsius": 22}, and the model is never asked
Loading

Nothing is captured from a frame: suspension is replay. A durable handler finds each recorded result by the op's name. It serves an op it has a result for and runs an op it has none for. A changed prompt or tool argument keeps the name, so it is served the recorded result; a changed result type validates that result against the new type, and fails the run if it does not fit. A test is where a change in which ops are yielded, under which names, or in what order is caught: wiki/concepts/testing.md.

The handler and its engines

DurableHandler runs a workflow: each model call, tool call and ledger append is a checkpointed step of a task, and a wait is the engine's own await. The engine holds the tape:

engine for
Absurd on Postgres many workers over one queue (vendored and pinned in infra/absurd)
embedded SQLite (effective.engines.sqlite) one process and one file, with no service to run

The two engines run one conformance suite through the same handler (tests/_conformance.py), and the durable tests crash a run at every op and resume it. Testing a workflow needs neither: two in-memory handlers serve canned results and replay a recording (wiki/concepts/testing.md).

The handler is the semantics

The op set is the syntax of a small language, and a handler is its transition relation: each op steps the run by an answer from the tape when the tape holds the op's name, and from the world otherwise. Record, replay and the two durable engines are four refinements of that one relation, and for workflows whose rows have distinct ids they are meant to agree on the ledger a run leaves, up to the order in which concurrent branches commit. wiki/concepts/semantics.md states the rules and grades each law: proved in Lean, model-checked in Quint, tested, or open.

Context, evals and telemetry

Each place a workflow meets a model is a named, typed seam that a deployment can tune and meter:

seam what it is read
prompts a t"..." whose interpolations are the context's regions: inputs render in, outputs parse back wiki/concepts/context.md
cache order the stable prefix first, the volatile part last; within a role, render refuses the other order wiki/concepts/context.md
skills a catalog in the cached prefix, a disclosed body in the volatile tail, activated once above a fan-out wiki/concepts/context.md
code and files code a model writes runs as an effect in a sandbox; the coder's shell runs in a container wiki/concepts/context.md
evals judgments, improve over a Pareto frontier, and a recorded run scored again, replayed, or forked at one decision wiki/concepts/evals.md
keys and projections every op's name composed by one grammar; views folded from the append-only ledger wiki/concepts/tapes.md, wiki/concepts/graph.md
telemetry OTLP spans under the OpenTelemetry GenAI conventions, each placed span carrying the key that joins it to the run wiki/concepts/tapes.md

Building with a coding agent

Effective is for building repeatable agentic workflows with a highly capable coding agent such as Claude Code, often from a one-off: a task the agent has done once, whose session telemetry shows the steps to make repeatable. The agent writes the workflow and its tests; a workflow that performs no I/O runs offline against canned answers, and every seam has a backend that runs in the agent's own sandbox (SQLite for the engine, Monty for sandboxed code), so the agent checks its own work with no container, no key and no spend. The person decides what the workflow may do: which calls need approval, what a run may spend, and what an answer must satisfy. Each of those is a value in the code, and a recording or a fork shows the person what the workflow does. docs/co-development.md walks through the three stages, from a first version to a deployment, and the check that closes each.

What's in the box

All under src/effective/ unless noted.

capability where
the op set and the authoring API: step, ask_llm, call_tool, await_event, await_until, sleep_until, append_ledger ops.py, domain.py, api.py
typed prompt channels: render, Gated, Repair, cache and role directives, skill disclosure channels.py, skills.py
identity: every op key composed from a t-string by one injective grammar, and numbered where it repeats, so two positions in a run never share one keys/
concurrency: gather, race, quorum api.py, choice.py
combinators: recurse, route, descend, fixpoint and mutual (loops in constant stack), respawn (a long loop as a chain of tasks), hoisted (a skill activated once above a fan-out), and sandboxed code as an effect (run_code, on Pydantic Monty) combinators.py, code.py, monty.py
judgments: judge and select ask typed questions from a t-string judgment.py, api.py
layers: retry, permission, budgets and telemetry as generator middleware, and govern to compose them layers.py, govern.py
the ReAct agent loop, with a typed answer repaired inside the loop, interrupts, steering and in-flight cancellation react.py, interrupts.py, steering.py, cancel.py
child tasks, and a parent that hears how its child ended spawning.py
permission: a fail-closed cascade of rules and a human permission.py, parked.py
cost: metered model calls, spend caps that survive a crash, a cache cost.py, budget.py, spend.py, cache.py
telemetry: OTLP/JSON spans under the OpenTelemetry GenAI conventions, joined to the run by key telemetry.py
counterfactuals: fork a recorded run at an op, change one result, run the rest live fork.py, counterfactual.py
optimization: improve (reflective prompt evolution) over a Pareto frontier of cost, quality and latency improve.py, pareto.py, and the benches in src/agent/
the read side: a run as a graph, cards, a dashboard, a terminal viewer graph/, cards/, dashboard.py, runview.py, src/tui/
interpreters for ops: OpenAI, the claude -p and codex exec CLIs, a judge service, a shell, the web interpreters/

Not yet in the box

missing where it stands
memory between runs a run's record is durable and forkable, and nothing carries what one run learned into the next; open is whether recall is a recorded op or a layer that fills the prompt, and how a write to memory is authorized
MCP servers as tools an MCP tool would reach the loop through an interpreter for CallTool, under the permission cascade, with its result recorded; not built, and the coding example does not implement them
a deployment from zero a walkthrough from a fresh managed Postgres to a running worker is not written

Examples

run what it is needs
uv run python examples/react_toy.py the agent loop above, driven by a script nothing
uv run python examples/hooks_as_layers.py the audit layer above, around the first workflow nothing
uv run python examples/first_workflow.py the first workflow nothing
uv run examples/smol_agent.py <url> a whole agent in a few lines, ported from Thomas Schranz's smol.clj an OpenAI-compatible Responses endpoint
uv run python examples/smol_durable.py the same house agent, durable and interruptible, with a judge guarding the door (examples/smol_door.py) nothing: offline without keys; OPENAI_API_KEY and JEV_API_KEY for a real model and judge
uv run python -m examples.coder PROJECT GOAL a small coding agent whose test suite decides when the work is done (src/examples/coder/README.md) OPENAI_API_KEY, Podman
uv run deep-research run "<question>" cell="..." a frontier search over web leads, stopped when two hosts agree on every cell (src/examples/deep_research/README.md); uv run deep-research demo researches a fictional probe offline TAVILY_API_KEY (or BRAVE_SEARCH_API_KEY with --search brave), JEV_API_KEY, the claude CLI; nothing for demo
uv run python -m examples.agentfirm sla-cap an accounts-receivable firm: a payment short of its invoice is applied, the credit the contract grants is approved by someone other than its proposer and posted through an ERP port, and what is still owed is supervised (src/examples/agentfirm/README.md) nothing; --odoo settles on Odoo Community, after just odoo-up
just tui-demo, just dashboard the terminal viewer and the run dashboard over a demo run the tui extra for the viewer

The agent firm

A firm exists, Coase argued in 1937, because some coordination costs less by authority than by negotiating each exchange: work that repeats, and commitments someone must answer for. Agents doing a firm's work face the same trade. The agent firm, src/examples/agentfirm/, gives them a firm's controls for one accounts-receivable job, resolving a short payment. Bob's company paid $70,000 of a $100,000 invoice, deducting $30,000 for missed service levels. An AR clerk model proposes a credit, and a reviewer model checks it against the contract; the contract computes $12,000, a controller approves it within their limit, the credit memo posts to the ERP, and a collector works the $18,000 still owed. Every action has an owner, an approval where policy requires one, and a record that survives a crash. uv run python -m examples.agentfirm sla-cap runs it offline with no keys; its README walks through the case, the run's record and how to set the firm up for your own book.

Configuring a run

A run is configured by a config: named, typed values bound in code, with layers over them from the command line or the host. effective.config serves any program built on effective, and reads best imported as cfg:

cfg.given(name, value) binds a dotted name to a typed value
cfg.declare(name, schema) declares a name a layer must fill
cfg.union(*parts) joins parts that bind different names or agree, and refuses two values for one name
cfg.over(base, change, layer=L) lays a named layer's values over a base that declares them, recording L in each name's history
cfg.resolve(config, Model) reads the config as a pydantic model
cfg.digest(config) identifies the config's values, for a run to record
cfg.SecretRef(...) names a credential, which a handler resolves at each call; the config never holds it

The command line is one more layer (effective.cli), and so is the host's environment, read and recorded by effective.config.pinned.pin.

A research run's settings are one config. RESEARCH gives every setting its default, among them search, the search API a run uses, and reconcile, the model that judges claims or None; it declares the question and leaves it unbound. Research is the model a run reads it as, and the durable task a run spawns keeps that model as its params, so whichever process runs the task reads its settings from there. From src/examples/deep_research/settings.py:

RESEARCH = cfg.union(
    cfg.declare("question", cfg.STR),
    cfg.given("steps", 6),
    cfg.given("model", "haiku"),
    cfg.given("search", "tavily"),
    cfg.given("held", False),
    cfg.given("searches", SEARCHES),
    cfg.given("budget", BUDGET),
    cfg.given("pick", "worth"),
    cfg.given("second_source", True),
    cfg.given("until_settled", True),
    cfg.given("reconcile", None, cfg.optional(cfg.STR)),
)
...
class Research(BaseModel):
    """One run, as `RESEARCH` and its layers resolve; each `cells.NAME` is a key of `cells`."""

    model_config = ConfigDict(frozen=True, extra="forbid", use_attribute_docstrings=True)
    question: str
    """What is researched: a subject, such as a framework's name."""
    cells: Cells
    """The facts a satisfactory answer states, each `name="what it asks"`."""
    steps: int
    """Steps the search may take; each acts on the leads it picks."""
    model: str
    """The `claude -p` model that reads each page."""
    search: SearchProvider
    """The search API: Tavily with `TAVILY_API_KEY`, or Brave with `BRAVE_SEARCH_API_KEY`."""
    held: bool
    """Replay the searches kept in the run's directory, with no search key and no search spend."""
    searches: int
    """Search requests the run's directory may send, counting earlier runs there."""
    budget: Budget
    """The run's budget: dollars its model calls may report before the next is refused."""
    pick: PickName
    """How a step picks its leads: the two worth most, the first two queued, or every one."""
    second_source: bool
    """Send a search away from the one host that quotes a cell's value."""
    until_settled: bool
    """Stop once every cell is settled, rather than when the steps or leads run out."""
    reconcile: Reconciler | None
    """The OpenAI model that judges each claim's quote, with `OPENAI_API_KEY`."""

The framework tracker asks the same questions of each framework Effective is compared with, so the answers line up in one table. TRACKED joins the research's cells to RESEARCH, and a layer named for each framework fills the question; each cells.NAME becomes a key of cells. From src/examples/deep_research/tracked.py:

TRACKED = cfg.union(
    RESEARCH,
    cfg.union(
        cfg.given("cells.maintainer", "the company or project that maintains it"),
        cfg.given("cells.language", "the programming language of its main SDK"),
        cfg.given("cells.license", "the license its source code is released under"),
        cfg.given(
            "cells.durable", "whether a run resumes from its last completed step after a crash"
        ),
    ),
)
...
def framework(name: str) -> cfg.Config:
    """`TRACKED` asking about `name`, which is the layer that sets the question."""
    return cfg.over(TRACKED, cfg.given("question", name), layer=name)

uv run python examples/configured_research.py composes and resolves it, offline, and prints what follows. One framework's run, as the tracker reads it, and the layer that set its question:

langgraph = framework("LangGraph")
cfg.resolve(langgraph, Research) ->
{
  "question": "LangGraph",
  "cells": {
    "durable": "whether a run resumes from its last completed step after a crash",
    "language": "the programming language of its main SDK",
    "license": "the license its source code is released under",
    "maintainer": "the company or project that maintains it"
  },
  "steps": 6,
  "model": "haiku",
  "search": "tavily",
  "held": false,
  "searches": 24,
  "budget": 1.0,
  "pick": "worth",
  "second_source": true,
  "until_settled": true,
  "reconcile": null
}
cfg.history(langgraph, 'question') -> ('LangGraph',)

A digest identifies a config's values, whatever layers set them: a layer that sets steps to the 6 it already was keeps the digest, and one that sets it to 2 changes it, as another framework does:

cfg.digest(langgraph)                      -> ae0d3368d0e130b5
cfg.digest(over(..., given("steps", 6)))   -> ae0d3368d0e130b5
cfg.digest(over(..., given("steps", 2)))   -> 9093adefc9105891
cfg.digest(framework('Temporal'))          -> 1d41466459e151e0

The shared config alone is refused, since nothing has filled its question, and so is a layer that binds a name the config never declared:

cfg.resolve(TRACKED, Research) -> UnboundName: unbound question
cfg.over(TRACKED, cfg.given("cell.license", "MIT"), ...) -> Undeclared: undeclared cell.license by the layer 'typo'

uv run deep-research track researches each framework and prints the table, with --steps and --search as one more layer, cli. It needs the research command's keys, TAVILY_API_KEY for the default search. Its token and search caps are shared by every framework it tracks, and each framework's run has its own dollar budget: a framework a cap stops keeps the cells it settled, and the table's last column says why each run stopped. The agent firm's README shows a config standing up a whole run, with a credential named by reference.

Learn more

read for
docs/intro.md the model in ten minutes: generator, handler, tape, layers, combinators
docs/first-workflow.md the first workflow, line by line
wiki/concepts/testing.md testing a workflow, and how the substrate is verified: crash at every op, two engines, schedules, models and proofs
docs/effective-101.md the concepts in depth: the op set, the combinator algebra, the temporal shapes
wiki/concepts/semantics.md the handler as the transition relation, and how each law is known
wiki/concepts/rosetta.md DSPy, Pydantic AI and LangGraph, term by term, with a DSPy module ported
docs/co-development.md building a workflow with a coding agent, stage by stage
wiki/index.md one concept a page, and the index of architecture decisions
docs/CHANGELOG.md what a caller must change when upgrading, release by release
wiki/references.md the work Effective builds on: Recursive Language Models, GEPA, ReAct, Absurd, tdom and more

Requirements

need for
uv and git everything; uv installs the pinned Python 3.14 (.python-version)
just the recipes; just alone lists them
rootless Podman the durable lane (a digest-pinned Postgres), Odoo Community for the AR firm, the coder's sandbox, rendering diagrams, the sandboxed formal checks
elan (Lean), node and npm, a JDK 17+ the formal gates only (just formal-setup, just formal)
extra for
tui the terminal viewer (textual, rich)
judge the Jev judge interpreter
bridge the arrivals bridge (redis)
coding the lint and the coding machine's tools (ast-grep)
binary psycopg with its own libpq, for a machine without one

Contributing

just check        # the gate: lint, ty, the docs and key checks, the suite
just pgt-up       # the test Postgres under Podman, for the durable lane
just formal       # Lean proofs and the Quint typecheck
just odoo-up      # Odoo Community under Podman; with `pgt-up`, `just odoo-test` runs the AR firm against it

How the substrate is verified, from crash sweeps to the formal models, is on the testing page (wiki/concepts/testing.md).

Run just check from a git clone: its docs gate reads git ls-files. It needs the test Postgres (just pgt-up); without one, a test session refuses to start rather than skip the Postgres-backed tests. EFFECTIVE_SKIP_PG=1 runs without it. AGENTS.md is the contributor guide, for people and coding agents alike: the layout, every recipe, the invariants and the code-prose rules.

License

MIT. See LICENSE. Copyright (c) 2026 Jim Baker.

About

Durable, replayable agentic workflows in Python: algebraic effects as composable generators, PEP 750 t-strings as typed prompt channels.

Topics

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages