Skip to content
Animated Visuals

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)                                       # 6

Five 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 · simulated

Fig. 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 · mechanism

This 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 · mechanism

States 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 · simulated

This 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 post

Means 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