Cloud Agents Should Use (Some) Code Execution | Lexifina<br>Skip to main contentFeaturesSecurityPricingBlogChangelogSign in
Blog/Research/Code-use tools
Blog/Research/Code-use tools
Cloud Agents Should Use (Some) Code Execution<br>Alan Yahya·July 31, 2026·6 min read
There are two ways to make an agent faster: call the model fewer times, or trim the repeated work from the calls that remain. This approach mainly addresses the former.<br>Tool calls are not intrinsically expensive; the cost comes from repeatedly injecting their results into a synchronous model loop. The difference becomes clear when we compare how often each approach crosses the provider boundary.<br>Tool approachThree model calls<br>Model: decide what to read<br>-> read_section<br>Model: interpret the text and draft the edit<br>-> propose_edits<br>Model: read the staging result and write the final answer<br>SDK approachOne model call<br>Model: judge, draft, write the restricted program and response<br>-> local SDK locates and validates the target, then stages it<br>Remove Repeated Model Work<br>A small, restricted code cell avoids much of that repetition by handling mechanical work such as searching, filtering, comparing, aggregating and transforming data inside one execution. The model still decides what matters and interprets the result, while the host application controls permissions, approvals and merges.<br>Intermediate pages, duplicate matches and irrelevant records stay inside the execution environment, so the model sees only what it needs for the next decision. This is most useful when the mechanics repeat but the path varies: keeping only matching records or stopping as soon as the answer is clear.<br>Keep the Remaining Context Small<br>Reducing model calls is only one part of the optimisation. The calls that remain should also carry less context: tools can be loaded only when needed, and large outputs can be filtered before they reach the model.<br>Keeping model-visible history append-only and tool ordering stable makes more of the prompt reusable, while applying approval rules at runtime avoids changing the tool definitions. The same foundations make subagents easier to deploy: each can inherit a stable, cacheable base configuration while runtime policy narrows its permissions for the task.<br>How an Edit Moves Through the System<br>The code cell can prepare an edit, but it cannot apply one directly. The change moves through three controlled steps:<br>1The model decides and drafts<br>The model gets enough context to find the clause, decide the change and draft an explanation, but no access that could alter the document.
2The code finds the right section<br>The SDK finds one exact target and builds the edit plan locally, without another model call.
3The host checks and stages the edit<br>Lexifina validates the document, snapshot, target and edit plan before staging it for human review. If validation fails, the draft response is discarded.
Lexifina's guardrails stay active throughout. If the program needs an authorised host tool, it pauses while the application checks permissions and returns a verified result, then resumes without another model call.<br>Results Across 100 Samples<br>We compared both approaches across 100 runs, spanning rewrites, discovery, risk analysis, reasoning and co-ordinated edits. Documents, prompts, model family, SDK, tool policy and the no-apply rule stayed fixed.<br>MetricEarlier-path medianCode-use medianChangeAgent execution129.019s83.373s−35.4%P90 agent execution164.107s111.625s−32.0%Tokens including cache creation19,69410,924−44.5%<br>Comparing one run<br>Select any bar or marker to see the saved program, payload, result and timing data.<br>0s5s10s15s20s25s30s33s
Full run<br>Complete run · 32.773s
Model calls<br>Read decisionEdit decisionFinal response
Host tools<br>ReadPropose
Host response<br>Emit
Milestones<br>staged 16.554sanswer 30.131s
Full run<br>Complete run · 14.524s
Model call<br>One model call
Local code<br>SDK
Host tool
Host response<br>Release
Milestones<br>staged 6.901sanswer 11.886s
Total timeModel callLocal codeHost toolSaved result<br>When to Use Code<br>Use it when<br>The task needs deterministic search, comparison or transformation over several document values.<br>Several local operations can replace repeated model-facing tool calls.<br>The result must target the exact text and be easy to audit and review.
Use ordinary tools when<br>A short answer or one ordinary read already supplies the result.<br>The document is too large to provide enough initial context and there is no plan for reading it in parts.<br>A required validation step must run before any response can be released.
There is a crossover point. Generating and validating the program, then starting the sandbox, creates a fixed cost that only pays off when there is enough local work or enough model round trips to avoid. One or two small sequential calls may still be faster as ordinary tools.<br>Code execution should not be confused with delegation. A code cell keeps tool-heavy work inside one agent, while a subagent gets its own model context for...