Speculative programmatic tool calling
A visual guide to Alex L. Zhang, “Speculative Programmatic Tool Calling”, blog post, August 24, 2026 · the post · the code (MIT). Built with Animated Visuals; every measured number is from the post and says where.
Speculative programmatic tool calling (sPTC, Zhang's term) starts safe sub-model calls while a model is still writing the program that contains them. This guide follows one six-line program where line 2 starts three sub-model calls before the model finishes writing. It shows how a throwaway copy of the program runs those calls early, how their answers reach the real run, and when the method saves time.
The example program
Zhang tested the method on a Recursive Language Model (RLM), a harness where the main model never reads the long input directly. The input sits in a Python variable called context. The model writes code against it in a REPL, a Python session that keeps its variables from one cell to the next. llm_query makes one sub-model call. His benchmark, OOLONG, asks the model to label a long list of items and then count the labels. Its paper calls this classification and counting in context. The example below is one cell the model might write.
chunks = context.split("\n\n") # 1 plain code
labels = [llm_query("Label: " + c) for c in chunks[:3]] # 2 three sub-model calls
tally = llm_query("Count the labels: " + str(labels)) # 3 needs line 2
notes = read_file("notes.txt") # 4 reads a file
review = llm_query("Compare with my notes: " + notes) # 5 needs line 4
print(tally, review) # 6Five sub-model calls and one file read. The three label calls don't depend on each other. The tally needs all three labels. The review needs the file. Every figure below runs on these six lines.
How calls start early
Without speculation, the harness waits for the last line and then runs the cell from top to bottom. Line 2 makes three sub-model calls in a row because each one blocks until it returns. With speculation, the three label calls start together when line 2 is complete. The tally starts as soon as they return, while the model is still writing. Set each sub-model call to by dragging the number. The answer arrives at instead of .
Calls start while the model is still writing
Fig. 1 · simulatedFig. 1 simulates one turn; its writing speed, line endings, and call lengths are illustrative, not from the post. Drag across the lanes to move through the turn, and hover over a block to identify it; call length and writing speed are in settings.
The post describes two sources of savings. First, calls run during the writing instead of after it. Second, independent blocking calls run at the same time even when the code lists them one after another. Zhang calls this a naive JIT compiler, a compiler that rewrites code while it runs. In this turn, of the saving comes from running the labels at once and from overlapping with the writing.
The throwaway copy
Line 3's sub-model call needs line 3's input, which depends on line 2's output. The harness therefore keeps a shadow copy, a deep copy of the program's variables that runs each line as soon as it is complete. In the copy, a sub-model call starts in the background and leaves a promise, a placeholder that fills in when the answer arrives. Each call is filed under its inputs in a store of early calls. The real program can find the answer there later.
The copy never changes the real variables, and it is thrown away at the end of the turn. Zhang separates the two because the model may write broken code or stop in the middle of a call. The real program still runs the cell as one unit or not at all.
The copy runs ahead while the real program waits
Fig. 2 · mechanismThis figure shows the mechanism on the worked program, with the prompts shortened to “…”. I ran these six lines through Zhang's library, where four calls started during the stream and the real run found four answers before making one call itself; step through with the buttons or arrow keys.
Which calls can start early
Half-written code can run only when its effects cannot leave the copy. The post uses two rules. First, tools are declared up front. @spec.tool(speculatable=True, pure=True) marks a tool that may run early and has no side effects. A tool without those flags, such as read_file here, never runs early. Ordinary functions that reach outside the program, such as open, are also unsafe. Second, a call does not start when any of its inputs depend on something unsafe.
Calls are matched by their inputs and by how many times they occur. Five identical calls for a majority vote therefore receive five separate answers instead of reusing one answer five times. A started call can still be thrown away when the real program never asks for it. Pick a call and step through its states.
Calls are handed over, refused, or thrown away
Fig. 3 · mechanismStates from the post's design section, matched to the states a call goes through in Zhang's code (started, ready, claimed, thrown away). Pick a call in the tabs, then step through it.
No rollback is needed because a wrong guess never reaches the real variables. The cost of a wrong guess is one unused sub-model call, and those calls can add up. Zhang's worst case is a serving engine clogged with extra speculative requests. This can be controlled through how aggressive speculation is and how the calls are queued. The post says the copy itself is cheap. Checking the stream costs close to nothing, and the deep copy is small because REPLs tend to hold few large mutable objects.
When speculation saves time
The possible saving depends on two limits. In this program, overlap is capped by how long the model keeps writing after line 2. Running calls at once is capped by how many calls are independent. The gain is largest when the calls fit inside the writing, at . Past that point, the tally finishes after the writing and the gain settles toward 5 ÷ 3 ≈ 1.67× because five calls in a row became a chain of three. Drag the line. The band shows what the post measured on a real benchmark, well below the simulation's best case.
The gain peaks when calls fit inside the writing
Fig. 4 · simulatedThis figure is computed from the same simulated turn as Fig. 1; only the band comes from the post. Drag the vertical line, or focus it and use the arrow keys; the same control also moves Fig. 1.
Measured results
Zhang measured the method on his RLM harness with OOLONG, using the trec-coarse split with 132k tokens of context, and OOLONG-Pairs with 32k. vLLM served Qwen3-30B-A3B-Instruct on a node of eight H100s. He ran each setting 5 times at temperature 0.7 and at 0.0 to reduce run-to-run variance, with 4 and with 8 tasks sharing the same serving engine. His summary reports speed-ups of about 1 to 1.2×.
The plot gives a less precise picture. Mean wall time with speculation is lower in all four settings, and the ratios of the means are , above the post's stated range. The 95% intervals overlap in every setting. The speculative runs also made more sub-model calls in three of the four settings, so the two sides took different paths through the tasks. The bars therefore cannot isolate the effect of speculation. The supported summary is 1 to 1.2×, with the bars suggesting that it was probably faster but not showing precisely by how much.
Faster on average in every setting, with wide spread
Fig. 5 · from the postMeans and 95% t-intervals over 5 runs, read off the post's plot by pixel (about ±10 s). Above each pair is the ratio or change between the two means; hover a mark for its numbers.
The simulated example gains more because it represents one turn whose time is entirely calls. The benchmark tasks take about nine or ten turns each in the turns tab, 4 or 8 tasks share one serving engine, and speculative calls add load to that engine. Zhang says the speed-up is hard to pin down. It depends on tool latency, how many tokens the model generates, serving-engine load, and the choices the harness makes.
When to use it
Zhang recommends the method when the tools are slow sub-models or sub-agents. It is especially useful with locally run models, where decoding one main context leaves the hardware waiting on memory and extra calls can put it to work. Through a hosted API, the only gain is overlap. He expects larger long-term gains from JIT-style methods that overlap calls with the program's own execution as harnesses write longer programs.
Checked against the source
- Source. Alex L. Zhang, “Speculative Programmatic Tool Calling”, blog post, August 24, 2026, alexzhang13.github.io/blog/2026/spec-ptc. No paper version exists that I could find; alphaXiv lists the same post (2608.spec-ptc), not an arXiv paper. Code: github.com/alexzhang13/spec-ptc, MIT, read at commit 9b78b7d (August 24, 2026). No code or figure from either is copied here; the figures are redrawn.
- Benchmark setup (post, “Some Comparison Numbers on RLMs”): OOLONG trec-coarse at 132k and OOLONG-Pairs at 32k; one node of 8×H100 (the post writes “8xH100 80B”) running vLLM; Qwen3-30B-A3B-Instruct (the post's text says “Qwen3-30B-A3B-Instruct-0527”, its plot title “Qwen3-30B-A3B-Instruct”); temperatures 0.7 and 0.0; 5 runs per setting; 4 and 8 concurrent runs on one engine.
- 1 to 1.2×: the post's sentence under its results plot, “The speed-ups for the RLM are generally on the order of 1-1.2x.” The post does not say how that range was computed.
- Fig. 5 values: read by pixel from the post's plot (
spec-plot.png, 2640 × 1518), whose footnote says “Mean over repeats · 95% t-interval · all n=5 units included”. One pixel is about 4 s (temperature 0.0) or 9 s (0.7) of wall time, 5 sub-model calls, 0.03 turns. The plotting code clips interval ends at 0. The post publishes no per-run values (the CSV its plotting code reads is not in the repository), so the figure shows means and intervals, not one dot per run. The ratios (1.45×, 1.40×, 1.28×, 1.84×) and the sub-model call differences are computed by this guide. - Simulated: Fig. 1 and Fig. 4. Text before the code takes 4 s, the model writes 16 characters per second at 1× speed, the file read takes 0.2 s, and every sub-model call takes the same time (4 s by default). Real calls vary; nothing else contends for the engine. Figs. 2 and 3 show the mechanism, not timings.
- Mechanism checks: I ran the worked program through Zhang's library at 9b78b7d with a fake sub-model. Four calls started during the stream (three labels at once when line 2 closed, the tally after they returned),
read_fileran once, in the real run, and the real run had four hits and one miss (the review). A call whose input comes from a tool without the speculative flags is skipped, and later independent calls still start. - One mismatch between post and code: the post's Case 4 comments say a call after an
open()line is still “speculated after a”. In the code at 9b78b7d a blocked built-in likeopenstops the copy for the rest of the turn; I ran the post's Case 4 and the later call was not started early. The worked program uses a tool marked unsafe instead, where post and code agree. - Left out on purpose: “peeking”, where the copy also starts calls from a line that isn't finished yet (a loop body) and takes those bets back if later tokens change them; async tools, the out-of-process daemon and the plugins for other agents; the deterministic program suite in the repository, which the post chose not to report; the related work it cites (Conveyor, Speculative Interaction Agents, AsyncFC).
- Not verified: the author's posts about it on X; whether the plot's “Wall time (s)” is per task or per run (the plotting code reads per-task rows, but I did not trace the aggregation); why the plotted means differ by more than the stated 1 to 1.2×.