kleene@sql

Kleene: a relational calculus for language-model computation

Status: engineering write-up for the M0–M7 implementation. Section 3 is the deterministic test suite (scripted providers, code oracles, replay fixtures). Section 4 is the first run of the four shipped packs against a real model (Claude Opus 5.5, 27 September 2026); the per-task rows are plots/evals.csv.

1. Claim

An agent’s work can be written as queries. Retrieval is a scan or a grep table function, judgement is a boolean call function in a WHERE, delegation is a lateral table call that opens a child session, search is a recursive CTE with a beam LIMIT, and the answer is a relation. Once the work is a query, three things fall out that a tool-calling loop cannot offer:

  • A planner. Call predicates have a cost the optimizer can see, so join order, conjunct order, semi-joins for EXISTS, cascades through cheap proxies and beam-limited recursion are rewrite rules with a cost model, not prompt engineering. EXPLAIN shows the estimate before the spend.
  • Budgets as query semantics. Calls, tokens, dollars, depth and wall clock are dimensions of a Budget inherited and sliced by child sessions; a statement whose estimate exceeds what is left is refused with its plan.
  • Learning as tables. What the harness learns (playbook SQL, ratings, generator dials, sampled selectivities) is rows in DuckDB, inspectable, replay-gated and revertible.

2. The algebra

Ordinary operators (σ, π, ⋈, ∪, γ, μ) plus a call kind on any operator that evaluates a call expression: λ_f (scalar map-call, one call per distinct input), κ_g (expand-call, LATERAL g(...), output cardinality the branching factor), ρ_d (rlm / spawn, the child’s whole plan), and σ_llm / ⋈_llm (predicates containing calls). Cost is calls × per-alias tokens and dollars; selectivity comes from observed pass rates once a predicate has been seen.

The eight rewrite rules of the plan are implemented: cheap-first over filters and join conditions, memo dedupe, batching (a prompt function answers up to n distinct tuples per call, 20 unless BATCH n says otherwise, priced as ceil(rows / n) calls), cascade, semi-join, beam-limited recursion, join ordering with branch-and-bound over relation subsets, and volatility fences. Estimates mirror the executor’s left-to-right short-circuit evaluation, so cheap-first is visible in the numbers rather than assumed.

The complexity fragment of every statement is reported (CQ / FO / REC) as a badge; it is a reminder that a recursive call plan is a program, not a lookup.

3. What was measured, deterministically

PropertyTestResult
Relational core equals DuckDBproperty-based differential suitepasses, recursive CTEs included
Join ordering with two call predicates over three relationsestimate and actual calls against a counting sinkas written 12,000 estimated calls / chosen 60; actual ≥10× fewer with identical rows
Plan-space growthseven-way join665,280 bushy orders priced through 1,623 subset splits (309 pruned)
Cascadeproxy at 0.1× cost, thresholds (0.2, 0.8)100 proxy + 60 oracle calls beat 100 oracle calls by estimate; on data the oracle is asked only for the band (5 of 20)
Beam recursionexpand × 4, three rounds20 calls with beam 5 against >100 unbounded
Continual loopscripted solver over generated puzzlestasks solved, playbook v1 adopted after a replay eval, ratings and dial move, restart resumes
Terminal-Bench-style packshell tasks from SQLwrite_file and lines tasks solved, oracles run in fresh workspaces
Plain-agent baselinesame provider, JSON actionstool call then final; the control for cost parity

4. What the benchmarks measured

The four shipped packs ran once each under the three modes of docs/BENCHMARKS.md on 27 September 2026, with the default routing: root on claude-opus-5-5 at high effort, worker and judge on claude-sonnet-5, proxy on claude-haiku-4-5, priced at the list rates in the router. The per-task rows (kleene bench csv) are plots/evals.csv and the table is kleene bench report (plots/report.txt) over the store those runs left; the model replies were recorded but are not checked in, because the catalog issue below means they cannot replay the runs yet. dollars is the pack total, calls and tokens are per task, and the learning column excludes the gate replays (below).

packmodesolvedcalls / tasktokens / taskdollars
terminal (6)frozen6/62.1710,5510.07
terminal (6)learning6/61.336,7420.04
terminal (6)plain6/62.502,3950.03
oolong-like (20)frozen20/204.9030,7781.51
oolong-like (20)learning20/203.8541,9911.92
oolong-like (20)plain20/201.003,0840.39
finance-synthetic (20)frozen20/203.4542,1770.83
finance-synthetic (20)learning20/202.4029,9990.63
finance-synthetic (20)plain20/201.001,2440.07
legal-synthetic (20)frozen20/2012.6562,4521.69
legal-synthetic (20)learning20/2010.6058,3152.61
legal-synthetic (20)plain20/201.001,4750.11

What the numbers say, read with the caveats of docs/BENCHMARKS.md:

  • Accuracy is saturated. Opus 5.5 solves every task of every pack in every mode. The packs were sized for a weaker model and a dial of 0.5; they separate nothing on accuracy, so the comparison below is on calls and dollars only. Harder dials, longer contexts and real datasets are the next step.
  • The playbook cuts calls per task. Learning against frozen: terminal 2.17 to 1.33, oolong 4.90 to 3.85, finance 3.45 to 2.40, legal 12.65 to 10.60. On oolong the second half of the learning run took 3.9 calls per task where the first half took 3.8, so most of the gain arrived with the first adopted entries; on legal the halves are 13.7 and 7.5, the curve the design predicts. Only oolong (16 of 20 candidates) and legal (5 of 21) adopted anything: on terminal and finance every candidate was rejected by the gate because its replay cost more than the baseline’s, so their learning runs are frozen runs with ratings on, and the call reduction there is run-to-run variance, not learning.
  • The gate is the cost. Each candidate is replayed on three earlier tasks in both arms, six task runs per solved task. Those replays are recorded in playbook_evals, not evals: they cost $1.25 on terminal, $10.70 on oolong, $5.46 on finance and $16.63 on legal against $0.04, $1.92, $0.63 and $2.61 for the counted runs. At twenty tasks a pack the gate spends far more than the playbook saves; it earns its keep only over a long stream, and a cheaper gate (a smaller sample, a proxy model, a cached baseline) is the obvious next change.
  • The plain agent wins on cost here. Every context in these packs fits in one prompt (under 4,000 characters), so the tool-calling baseline answers in one call with no tool use: $0.39 against $1.51 on oolong, $0.07 against $0.83 on finance, $0.11 against $1.69 on legal. The SQL abstraction pays for a catalog, a planner and a transcript of rendered results on every turn, and at this scale that is pure overhead. The claim of section 1 is about work that does not fit in one call; these packs do not test it, and the numbers say so.
  • Per-clause calls are cheap and visible. On legal, the model classified clause by clause with a prompt function on the worker alias (up to 26 calls a task at about $0.10), which is the λ_f map-call of section 2 doing what it is for: many small calls on a cheap model, priced before they run.
  • Costs drift up within a run. Per-task dollars on oolong learning rose from $0.084 in the first half to $0.107 in the second while calls stayed flat. Two things grow the prompt: the adopted playbook entries, and the scratch tables every session creates in the shared store, which the catalog lists to every later session (287 such tables after these runs). The second was a bug: it also changed the prompt fingerprint from task to task, so a recorded run could not be replayed from its own fixtures. Bench sessions now drop their tables when they finish, so later runs do not carry it.

The other measurements of the design are in place but not yet exercised by these runs: transfer (learn on a prefix with --limit, then run the rest frozen), reverts (learn playbook), and the estimate and plan-space plots, which read trace_statements and were empty because these runs were made before bench run traced statements (it does now).

Packs shipped: tasks/terminal (Terminal-Bench-style), tasks/oolong-like (frozen corpus stream), tasks/finance-synthetic (frozen statements stream), tasks/legal-synthetic (frozen contracts stream). Harvey LAB imports through bench import-lab from a checkout; LAB’s evaluation.run_eval stays the scorer of record and the in-loop judge oracle approximates it. The external datasets themselves (OOLONG, Terminal-Bench, FinanceBench, CUAD, LAB) are not redistributed here. A coding pack (edit code in a workspace, judged by its tests) is the next pack to add.

4.1 Haiku 4.5 on the harder packs

The harder packs (tasks/coding, tasks/memo-rubric, tasks/logbook-hard) ran on 1 October 2026 with every solver alias (root, worker, proxy) on claude-haiku-4-5-20251001 and the judge alias on claude-sonnet-5-5 (the router file is plots/haiku-2026-10-01/router.toml). The sweep was stopped before it finished; the table is what completed. Rows and report are plots/haiku-2026-10-01/evals.csv and report.txt, the SVGs beside them. Columns as in the table above.

packmodesolvedcalls / tasktokens / taskdollars
coding (12)frozen2/124.5848,3750.87
coding (12)learning2/122.6729,9100.47
memo-rubric (20)frozen8/2019.9549,0431.30
memo-rubric (20)learning11/203.8023,0410.37
memo-rubric (20)plain8/203.1510,7640.35

Not in the table: logbook-hard in any mode (a frozen task took about 500 calls and over twenty minutes at Haiku’s pace, so the pack was capped at ten tasks and then stopped before the first row landed), and coding in plain mode (below). The gate replays, in playbook_evals, cost $1.43 on memo-rubric (11 candidates, 3 adopted) and $1.04 on coding (2 candidates, 1 adopted); the counted runs cost $3.36 in all.

What the numbers say:

  • These packs separate on accuracy. Where Opus 5.5 solved every task of the first four packs, Haiku solves 17% of the coding steps and 40 to 55% of the memos. The coding steps it solves are the first step of an episode (ledger-1-parse, inventory-1-stock); it solved no “extend” or “fix the bug report” step in either Kleene mode.
  • Learning beat frozen on memo-rubric, on both axes. 11/20 against 8/20, at 3.8 calls a task against 19.95 and $0.37 against $1.30. Two effects are mixed in. Three playbook entries were adopted and the second half of the learning run solved 5 of 10 where the frozen run’s first half solved 3 of 10. But most of the frozen run’s calls are a failure mode the playbook happens to steer around: Haiku writes LLM_JSON output schemas the API rejects (additionalProperties not set, minItems above 1, a bare {"hours": "number"}), and the statement retries until the call budget goes; five frozen tasks took 22 to 141 calls each. The adapter now rewrites a model-written schema into the accepted subset before the request (bare field maps, missing additionalProperties, minItems above 1), which would have removed most of that cost; these runs predate it.
  • Learning and frozen share the store’s memo. The learning run comes second in the same store, so a prompt identical to one the frozen run sent is answered from the memo for free. Two coding steps in the learning run finished with zero calls: they replayed the frozen run’s failed answers. The learning column’s calls and dollars are therefore not an independent measurement; a clean comparison needs a fresh store per mode, or the memo excluded from the gate’s cost accounting.
  • The plain baseline needs a protocol Haiku keeps. The agent asks for one JSON action per reply. Haiku answers with five to ten actions and invented tool output between them; the first extractor took the span from the first { to the last }, which never parsed, so on every coding task the model saw only “not a single JSON object” and gave up with a FINAL after two turns (0/12 at $0.45). The extractor now takes the first balanced object and runs it. With that, Haiku worked through the first coding task for all 30 turns without solving it ($1.11), and the rerun was stopped before a row landed. On memo-rubric, where the task fits in one call, the plain agent matches frozen on accuracy at a quarter of the cost, and learning beats it by three tasks for the same money.
  • The judge wobbles. Sonnet 5.5’s first lines included “PASS (with caveat)”, “PASS (provisional)” and “PASS or FAIL?”; the parser counted all of them as passes. It now requires a first line that says PASS and does not mention FAIL. The memo frozen and learning runs predate that change; the plain run does not.
  • Two provider facts. Haiku rejects output_config.effort, which the adapter sent along with the rest of the family; the default routing puts proxy on Haiku at low effort, so every proxy call failed until the adapter learned to omit it. And claude-sonnet-5-5 is served now (it was not on 27 September).

4.1.1 Haiku 4.5, the remaining cells (6 October 2026)

The cells the October 1 sweep left empty ran on 6 October 2026 with the same routing (plots/haiku-2026-10-06/router.toml), one fresh store per pack and mode, after two adapter changes: the conversation is now cached turn by turn (a breakpoint on the last message, not only the system prefix), and the structured-output schema drops maxItems, which the API rejects and every batched LLM_JSON call used to set. Rows and report are plots/haiku-2026-10-06/evals.csv and report.txt.

packmodesolvedcalls / tasktokens / taskdollars
oolong-like (20)frozen14/2050.4053,2170.75
oolong-like (20)learning19/201.959,1150.20
oolong-like (20)plain20/204.1524,9320.49
finance-synthetic (20)frozen20/207.2530,2740.43
finance-synthetic (20)learning19/207.6018,9630.29
finance-synthetic (20)plain20/201.655,3750.13
legal-synthetic (20)frozen14/2043.5048,5940.72
legal-synthetic (20)learning17/209.9516,5410.29
legal-synthetic (20)plain15/201.856,2270.16
terminal (6)frozen6/63.0011,1900.07
terminal (6)learning6/62.178,1590.05
terminal (6)plain6/62.507,2860.05
coding (12)plain4/1228.58218,0190.78
logbook-hard (10)frozen1/101,702.10476,2135.13
logbook-hard (10)learning1/101,694.40591,4435.95
logbook-hard (10)plain2/1022.201,365,3434.58

The counted runs cost $20.07, of which logbook-hard took $15.66; the gate replays (62 candidates, 22 adopted) cost another $5.64. What the numbers say:

  • On the original packs Haiku is close to Luna, at ten times the price. Learning beats frozen on oolong-like (19 against 14 at 2 calls a task against 50) and legal (17 against 14 at 10 against 44), as it did for Luna; the one-call plain agent is perfect on finance and oolong-like.
  • Logbook-hard is a worker-call storm. The frozen run made 79,324 worker calls of about 37 tokens each, $4.30 of its $5.13: Haiku writes a scalar llm() per row over the 30k-token log, and when it does batch, it answers with the wrong number of elements two times in three, so the harness falls back to one call per row. Two tasks took 5,000 and 8,500 calls. The root session itself was cheap (121 calls, $0.83, 1.2M tokens read from cache against 36k billed in full). This is the fallback the Luna run flagged, now with a price on it. The fix landed the same day (section 4.3): prompt functions batch 20 rows per call unless told otherwise, and a short batch answer is re-asked by item instead of one call per row.
  • Caching works where the prompt is long enough. Haiku’s minimum cacheable prompt is 4,096 tokens, so terminal and the per-row worker calls never cache; everywhere else the root session reads its history from cache from the third turn on.

4.2 GPT-6 Luna on the harder packs

All seven packs ran on 5 October 2026 with every solver alias on gpt-6-luna (OpenAI, chat completions, default reasoning effort) and the judge alias on claude-sonnet-5-5; the router file is plots/luna-2026-10-05/router.toml. Luna is priced at $0.10 per million input tokens and $0.50 per million output, a tenth of Haiku. Each pack and mode ran in its own store, so the learning columns are independent of the frozen ones this time. logbook-hard was capped at ten tasks after the Haiku run; its learning run was stopped after three tasks, with the second task alone taking 1,705 calls and running out of turns. Rows and report are plots/luna-2026-10-05/evals.csv and report.txt, the SVGs beside them.

packmodesolvedcalls / tasktokens / taskdollars
oolong-like (20)frozen15/2033.9519,2300.04
oolong-like (20)learning20/201.304,5160.00
oolong-like (20)plain14/201.002,0310.01
finance-synthetic (20)frozen14/204.1511,7890.01
finance-synthetic (20)learning15/203.4512,0810.01
finance-synthetic (20)plain20/201.009500.00
legal-synthetic (20)frozen18/2015.6523,9730.03
legal-synthetic (20)learning15/204.2515,8930.01
legal-synthetic (20)plain20/201.001,0040.00
terminal (6)frozen6/61.504,1280.00
terminal (6)learning5/61.504,2190.00
terminal (6)plain6/62.331,5840.00
coding (12)frozen11/1212.0855,2860.02
coding (12)learning10/128.1742,0940.02
coding (12)plain9/1210.2527,8600.03
memo-rubric (20)frozen8/2013.6036,5710.03
memo-rubric (20)learning18/206.1027,6830.02
memo-rubric (20)plain11/201.001,9780.01
logbook-hard (10)frozen7/10247.30180,7360.24
logbook-hard (3 of 10)learning1/3686.67746,2590.19
logbook-hard (10)plain3/102.3097,0000.07

The counted runs cost $0.74 of Luna in all; the playbook gate’s replays (84 candidates, 17 adopted) cost another $0.31, and the Sonnet judge’s calls are not traced, roughly a dollar at the most. What the numbers say:

  • The four original packs separate on accuracy with Luna. Opus 5.5 solved every task of these packs in every mode; Luna does not, so they measure something now. The one-call plain agent is perfect on finance and legal, where the context fits and the question is one lookup, and weakest on oolong-like (14/20), where aggregation over the whole context is the task. Learning on oolong-like solved 20/20 at 1.3 calls a task against frozen’s 15/20 at 34: the adopted playbook entry carries the aggregation query, and the rest is a lookup.

  • Luna solves what Haiku could not, for a tenth of the money. 11 of the 12 coding steps frozen against Haiku’s 2, including every “extend” and “fix the bug report” step but one; 7 of 10 logbook tasks frozen where Haiku never finished one. Each coding step cost about a fifth of a cent.

  • Learning beat frozen on memo-rubric again, now cleanly. 18/20 against 8/20 at 6.1 calls a task against 13.6, in separate stores, so no memo replay is hiding in the learning column. Four memo entries were adopted out of 18 candidates; both halves of the learning run solved 9 of 10, so the gap to frozen is there from the start and is mostly the shorter route the playbook gives. On coding, learning cost fewer calls (8.2 against 12.1) and lost one task.

  • Plain is the cheap mode where the context fits, and no further. On memo-rubric the one-call agent solves 11/20 for half a cent; on logbook-hard, whose contexts are around 30k tokens, it gets 3/10 and on coding 9/12 with a tool loop. Frozen beats it on every pack on accuracy.

  • Logbook-hard is where the calls go. Frozen tasks took 113 to 719 calls each, nearly all of them worker calls from batched LLM_JSON extraction over the log; Luna sometimes answers a batch of 20 with the wrong number of elements, and the harness then retries one call per row. Learning was worse, not better: the one task it finished past the first took 1,705 calls and no FINAL. The per-row fallback is the next thing to fix on this pack, for any model.

  • Three provider facts, all now in the adapter. OpenAI rejects max_tokens for every GPT-5 and GPT-6 model (the adapter sends max_completion_tokens to api.openai.com); strict structured outputs want an object root with every property required and additionalProperties: false (an array or scalar schema is wrapped and unwrapped); and a model-written schema with a typeless property or a partial required list is rejected outright, so the adapter completes it. Before those fixes a Luna run could not make a single LLM_JSON call.

4.3 Opus 5.5 on the harder packs (6 and 10 October 2026)

The three hard packs Opus 5.5 had not run yet, in all three modes, with the routing of the 27 September run (root on Opus 5.5 at high effort, worker and judge on Sonnet 5.5, proxy on Haiku 4.5; plots/opus-2026-10-06/router.toml), one fresh store per pack and mode, the conversation cached and prompt functions batched by default. Rows and report are plots/opus-2026-10-06/evals.csv and report.txt. Coding and memo-rubric ran on 6 October; logbook-hard, capped at ten tasks like the Haiku and Luna runs, on 10 October from the same router file, with its own plots and console under plots/opus-2026-10-06/logbook-hard/.

packmodesolvedcalls / tasktokens / taskdollars
coding (12)frozen12/128.5876,5331.31
coding (12)learning12/128.0088,9841.70
coding (12)plain12/129.4268,1561.32
memo-rubric (20)frozen10/205.3040,0861.26
memo-rubric (20)learning4/205.3544,0641.24
memo-rubric (20)plain10/201.005,7630.55
logbook-hard (10)frozen10/108.5079,2961.45
logbook-hard (10)learning10/106.7066,9971.43
logbook-hard (10)plain5/1011.90801,80314.82

The counted runs cost $25.08, $17.70 of it on logbook-hard and $14.82 of that on its plain run; the gate replays (26 candidates, 13 adopted) cost another $10.63, $7.67 of it on coding alone, so a learning run of coding costs five times what its rows show, while on logbook-hard the gate added $1.41 (10 candidates, 5 adopted, 6 replays). What the numbers say:

  • Coding is saturated for Opus, as the original packs were. Every task in every mode, where Luna solved 11, 10 and 9 and Haiku 2, 2 and 4. The three modes cost the same within a few cents, so the pack no longer separates modes at this model size.
  • Memo-rubric is the one pack where Opus is behind Luna. Half the tasks in frozen and plain against Luna’s 8 and 11, and learning mode made it worse: the adopted playbook entries pulled Opus from 10 to 4 solved at the same cost. Luna’s learning run on the same pack went the other way (8 to 18). The rubric judge is Sonnet 5.5 in both runs, so the difference is in what the models write, not in how it is scored.
  • Logbook-hard is where the SQL abstraction pays for itself. Opus solves all ten tasks in frozen and learning mode at 8.5 and 6.7 root calls a task and about $0.14 each, with no worker calls at all: it reads the 30k-token log through ctx and aggregates in SQL rather than extracting with LLM_JSON, where Haiku made some 1,700 worker calls a task and solved one. The plain agent on the same model solves five of ten for ten times the money: three tasks ran out of their thirty turns at over $3 each, and every turn re-sends the log, so a task averages 800k tokens against 79k. Frozen beats plain on this pack with every model that ran it (Luna 7/10 to 3/10, Opus 10/10 to 5/10); only Haiku, which could not finish the SQL route, is the exception.
  • The caching and batching changes hold at Opus prices. Learning coding read 5.0M tokens from cache against 30k billed in full; the batched prompt functions kept the worker side to a few hundred calls per run, where the Haiku logbook-hard runs made tens of thousands, and Opus on logbook-hard needed none.

5. Honest gaps

  • One run per pack and mode. Opus 5.5 solved every task of the first four packs, so section 4 compares cost there; the harder packs of section 4.1 separate on accuracy with Haiku 4.5, but only two of the three finished, and the learning column shares the frozen run’s memo.
  • Session scratch tables persist in the shared store and appear in every later session’s catalog, which grows prompts over a run and breaks replaying a recorded run from its own fixtures.
  • The replay gate costs six task runs per solved task and dominates the cost of a learning run at pack scale.
  • Threshold calibration from a sample is not built; thresholds are declared.
  • The learned cost model persists only in-session (sampled selectivity), not as versioned tables.
  • The plain baseline now uses native tool calling (every tool_use block of a reply runs, the answer comes through a final tool) and the coding pack’s tasks carry a check that refuses a FINAL while python3 -m unittest -q fails; the Haiku 4.5 numbers in section 4.1 predate both and have not been rerun.
  • Output schemas the model writes for llm_json are rewritten into the provider’s accepted subset, but only the shapes seen so far (field maps, open objects, minItems); a schema outside that still fails the call.

6. Reproduce

cargo test --workspace --all-features          # every claim in section 3
cargo run -- repl < demos/planner/three_way.sql # the plan space, live
cargo run -- learn run --tasks 20 --generators puzzle,corpus
cargo run -- bench run tasks/terminal --mode frozen --record fixtures/terminal
cargo run -- bench run tasks/terminal --mode plain --record fixtures/terminal-plain
cargo run -- bench report
cargo run -- bench plot plots/

The runs of section 4 are plots/evals.csv and plots/report.txt, with the SVGs bench plot drew from the same store beside them. fixtures/ is ignored by git: record your own with --record, and replay with --replay once the catalog caveat in section 5 is fixed.