Manuscript Offprint

arXiv:2611.01446

I-PTC: Interactive Programmatic Tool Calling for Stateful Tool-Augmented Agents

Huanzhi Mao, Chengkun Cao, Shuo Yuan, Joseph E. Gonzalez

Abstract

Large language models are increasingly capable of writing and executing code, suggesting a natural paradigm for tool-augmented agents: expressing tool use as programs rather than isolated calls. Programmatic tool calling (PTC) captures this idea through loops, batching, abstraction, and structured computation, but existing PTC approaches are largely stateless, limiting their ability to reuse intermediate results and adapt over time. This is problematic for real-world tasks, where tool environments and problem-solving processes are inherently stateful. We therefore present I-PTC, an interactive framework that executes stateful PTC: models issue code snippets that call tools, maintain reusable state, and evolve the tool interface through API corrections. To evaluate the effectiveness of I-PTC and the general PTC setting, we introduce PTC-BENCH, a benchmark of long-tail, production-style user requests whose difficulty comes from large initial environments, many dependent API interactions, extended planning, and recovery across follow-ups and transient failures. Experiments on PTC-BENCH show that I-PTC improves performance on complex multi-step tasks with large tool sets while substantially reducing token consumption over conventional tool-calling baselines and the original PTC baseline, highlighting stateful programmatic tool calling as a promising direction for scalable tool-augmented agents.

1 Introduction

As LLMs are asked to complete increasingly complex workflows over databases, search engines, code environments, and domain-specific APIs, the key bottleneck is no longer just tool selection, but the tool-use interface itself. Conventional tool calling emits one structured call at a time and returns each result to the prompt, which works for short tasks but becomes inefficient for long workflows: each call may require another inference step, intermediate results consume context, and operations such as looping, filtering, aggregation, branching, and joining API results must be simulated through repeated natural-language reasoning. Programmatic tool calling (PTC) [1-3] addresses this by letting agents write executable programs that orchestrate tools with loops, abstraction, conditionals, and structured computation outside the language context.

Figure

Figure 1: Pass rate vs. trajectory tokens for ten models on PTC-BENCH

While PTC improves complex tool use, standard execution remains brittle. One-shot programs must assume API behavior, return formats, and edge cases before seeing any outputs; if these assumptions are wrong, the agent may need to restart. Splitting the workflow into smaller blocks helps exploration, but loses intermediate variables and cached results, causing redundant computation and repeated tool calls. Thus, PTC reduces context pollution but can limit observability and error recovery.

Project code is available at https://github.com/HuanzhiMao/I-PTC.

We therefore propose I-PTC, an interactive programmatic tool-calling framework that gives the model a persistent notebook-like execution environment [4, 5]. In I-PTC, the model can issue code snippets, call tools, inspect intermediate variables, debug execution errors, and continue from a preserved namespace. This design retains the token-efficiency advantages of PTC while recovering the observability and error-recovery benefits of conventional tool calling. By separating execution memory from language context, I-PTC allows intermediate state to persist as executable variables rather than being repeatedly serialized into the prompt.

Beyond within-task persistence, I-PTC also evolves its interaction with the tool environment. On-demand tool inspection lets the agent dynamically acquire detailed tool information only when needed, reducing upfront prompt overhead when operating over large tool collections. Self-evolving APIs allow the agent to incorporate execution feedback into persistent API corrections and usage notes, so discoveries about tool behavior can be reused across later code snippets. Together, these mechanisms extend PTC beyond static program execution: they turn tool discovery, execution feedback, and recurring tool-use patterns into reusable task state, improving efficiency and reliability across complex workflows.

To evaluate I-PTC and the broader PTC setting, we introduce PTC-BENCH, a benchmark of 240 realistic user queries over simulated local APIs. PTC-BENCH targets production-style workflows such as scheduling, shopping, travel planning, record analysis, personal finance review, and customer support. These tasks share a common structure: large initial environments, many dependent API calls, extended planning under constraints, and reuse of partial work across follow-ups or transient failures. Unlike existing benchmarks that focus on function-call validity, tool selection, and short-to-medium-horizon interaction, PTC-BENCH provides a complementary testbed for evaluating tool-use interfaces on long-horizon, stateful workflows.

We evaluate I-PTC against conventional tool calling and one-shot PTC across PTC-BENCH, MCP-Universe [6] and MCP-Bench [7], using both open-source and proprietary model families. Across these settings, I-PTC improves task success on most evaluated models while reducing token usage and unnecessary tool calls (Figure 1), showing that interactive stateful execution provides a more scalable interface for complex tool-augmented agents. In summary, this paper makes the following contributions:

  • We propose I-PTC, a training-free, interactive programmatic tool-calling framework that gives agents a persistent notebook-like execution environment. I-PTC allows agents to iteratively execute code, inspect intermediate variables, debug errors, incorporate API corrections, and continue from preserved state, shifting PTC from stateless execution to stateful interaction.
  • We introduce PTC-BENCH, a benchmark of 240 long-tail, production-style user requests over simulated APIs, characterized by large initial environments, many dependent API interactions, extended planning under user-supplied constraints, multi-turn follow-ups, and recovery from transient API failures. The benchmark is interface-agnostic: tasks are defined by the user request and a deterministic verifier over the final answer and environment state.
  • We empirically evaluate traditional tool calling, standard PTC, and I-PTC across PTC-BENCH, MCP-Universe [6] and MCP-Bench [7] using both open-source and proprietary model families. Our results show that I-PTC improves task success on most evaluated models while substantially reducing token consumption and redundant tool use compared with conventional tool-calling and standard PTC baselines.

References (18)