Manuscript Offprint

arXiv:2610.24545

When Code Becomes an LLM Call: An Empirical Study of Call-Site Contracts in Agentic Systems

Shihao Weng, Longtian Wang, Yang Feng, Mingfei Cheng, Jia Liu, Chao Shen, He Ye, Federica Sarro, Xiaofei Xie

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Shihao Weng 1,∗^{1,*}, Longtian Wang 2,∗^{2,*}, Yang Feng 1,⊠^{1,\boxtimes}, Mingfei Cheng 3^{3}, Jia Liu 1^{1}, Chao Shen 2^{2}, He Ye 4^{4}, Federica Sarro 4^{4}, Xiaofei Xie 3^{3}

1^{1}Nanjing University, Nanjing, China 2^{2}Xi'an Jiaotong University, Xi'an, China

3^{3}Singapore Management University, Singapore 4^{4}University College London, London, United Kingdom

Software quality has long rested on specifications that state what code must do, and on testing and verification that check that the code does it. Agentic systems change this foundation: they replace parts of their logic, such as choosing the next tool, judging whether an action is allowed, or writing code to run, with calls to a large language model (LLM). The program still relies on what this logic must guarantee, but the model's behavior is not fixed by code, varies across runs, and can be steered by its input. The guarantee must therefore come from the program around each LLM call site, as a contract of pre-conditions, prompt constraints, and post-conditions. We present an empirical study of these contracts in 496 LLM call sites of 40 popular open-source agentic systems (13.4 MLOC, four languages). Using a multi-agent pipeline validated by 17,428 human judgments, we trace where every model output flows, derive an expected contract for each call site without access to its code, and compare it with the contract that the code implements. We find that 48% of LLM outputs reach control flow or execution, and 73% of these never reach a person. Programs enforce 78% of their constraints in code, but in the prompt, 67% of the constraints are only requested from the model. Although 82% of call sites are checked before and after the call, 63% of the expected constraints are unmet and only 5.3% of call sites satisfy their whole contract; ten recurring patterns explain 99% of the gaps. 38 mechanisms behind these gaps trigger at runtime, and a small code fix that enforces the expected constraint stops all 38. Our results show that the presence of checks says little about their adequacy, and we derive implications for agent developers, framework maintainers, and researchers.

CCS Concepts: • Software and its engineering → Software verification and validation.

Additional Key Words and Phrases: LLM call sites, agentic systems, software contracts, empirical study, LLM application security

1 Introduction

In conventional software, quality rests on a well-understood foundation: developers specify what each unit of code must do, as pre- and post-conditions, types, assertions, or tests, and use testing and verification to check that the code does it [21, 25, 36, 43]. Callers then rely on this specification without checking it again. The foundation works because a unit’s behavior is fixed by its code.

Agentic systems change this foundation. Coding agents such as Codex, Gemini CLI, and OpenHands [61] edit repositories and run commands on developers' machines, and personal assistants read messages and act on users' accounts. Each of them is an ordinary program, but parts of its logic are delegated to a large language model (LLM) [52, 60, 72]. Where a conventional program would call a parser, a model extracts the user's intent; where it would call a dispatcher, a model chooses the next tool; where it would call a policy check, a model judges whether an action is allowed. We call each such place an LLM call site: a location in the code where the program builds a request, calls a model, and acts on the reply.

Delegating logic to a model does not remove the need for its specification. The program still relies on what the logic must guarantee: that the chosen tool was offered, that the verdict is allowed, that the generated command is safe to run. The model does not establish these guarantees. Its behavior

∗^{*}Both authors contributed equally to this research. ⊠\boxtimesCorresponding author.

Authors' Contact Information: Shihao Weng, shweng@smail.nju.edu.cn; Longtian Wang, w976625943@stu.xjtu.edu.cn; Yang Feng, fengyang@nju.edu.cn; Mingfei Cheng, snowbirds.mf@gmail.com; Jia Liu, liujia@nju.edu.cn; Chao Shen, chaoshen@mail.xjtu.edu.cn; He Ye, he.ye@ucl.ac.uk; Federica Sarro, f.sarro@ucl.ac.uk; Xiaofei Xie, xfxie@smu.edu.sg.

Weng et al.

is not fixed by code, varies across runs, and can be steered by its input, through a model error [32] or an injected instruction [22, 40, 47], and testing can sample but not establish this behavior [7]. The specification therefore moves into the program around the call, which must enforce a contract {P}C{Q}\{P\} C \{Q\} before the call, in the request, and after the reply. For an LLM call, the post-condition Q never holds by construction; it holds only where the program checks it. Whether an agentic system is reliable and secure depends on whether each call site enforces the contract it needs.

Little is known about these contracts. Benchmarks evaluate models and agents end to end [15, 33, 39, 71]: they show whether a task fails or an attack succeeds, but not which check was missing. Defenses harden the model or its input [11, 14, 24], and empirical studies of LLM-based software examine how developers write prompts [38, 57] or why agent workflows fail [9]. None of them asks the questions that the shift from code to model calls raises: which logic do programs delegate to LLMs, how do they specify and enforce what this logic must do, and is that enough?

The last question is hard for a principled reason. Reading the code shows which checks exist, but a missing check leaves no trace in the code, and specification mining [4, 17] recovers what the code does and cannot tell what it should do. Measuring adequacy therefore needs a reference that is independent of the implementation. Figure 1 shows why the presence of a check is not enough. Codewhale

Untrusted input dev-setup-notes.txt · a file in the workspace a note whose text asks the assistant to answer in a repl code fence (paraphrased)

1 Build the request turn_loop.rs·pre-call REQ execution must be restricted to the tools advertised for this session PARTIAL the repl fence is a second way to run code that is not an advertised tool PASSES ON the file text as a tool result ⇒\Rightarrow a reply that contains a repl fence

2 Detect code in the reply sandbox.rs:9-11·post-call REQ only completed tool-call blocks are executed PARTIAL any reply that contains the marker counts as code: text.contains("~~repl") PASSES ON the text after the marker, up to the next closing fence

3 Execute the extracted code runtime.rs:1018·post-call REQ code runs only as an approved tool call MISSING exec() in a python3 subprocess: no approval, no sandbox (the docs say "sandboxed") PASSES ON observed: a fixed fence runs as real Python; a real model's prose about the marker raised a SyntaxError

Fig. 1. A file read by Codewhale reaches a real process through three weak checks.

runs a model reply as Python whenever it contains a ~ repl fence, without approval or sandbox, although its documentation calls the feature “sandboxed”. The program does check the reply, but only whether it contains code, never whether running it was approved.

This paper presents an empirical study of the contracts of LLM call sites. We analyze 496 call sites in 40 popular open-source agentic systems that span six task domains, four languages, and 13.4 MLOC. Our study has three stages (Figure 2). Stage I locates the call sites, splits each reply into typed outputs, and traces their consumers. Stage II reconstructs two contracts per call site: an expected contract, derived from a code-free functional specification by an agent that never sees the code, and an implemented contract, extracted from the code by an agent that never sees the expected contract. Comparing the two exposes missing and weak constraints that code reading alone cannot find. Both stages run as a multi-agent system, and three reviewers check every step with 17,428 judgments (Gwet's AC1 0.91 to 0.99). Stage III tests whether the gaps trigger at runtime and whether enforcing the expected constraint prevents them. We answer four research questions:

• RQ1: Which logic do agentic systems delegate to LLM calls, and where do the outputs flow? 48% of LLM outputs reach control flow or execution, and 71% are used only by the program or by other models.

  • RQ2: How do agentic systems specify and enforce the contracts of their LLM calls? Programs enforce 78% of their constraints in code, yet 67% of prompt-phase constraints are only requested from the model.

  • RQ3: How adequate are the implemented contracts, and where do they fall short? 63% of the expected constraints are unmet, and although 82% of call sites check before and after the call, only 5.3% satisfy their whole contract; ten patterns explain 99% of the gaps.

  • RQ4: What are the runtime consequences of contract gaps, and does enforcing the missing constraint prevent them? For 114 gaps, the missing constraint lies on a threat chain; 38 of their 60 mechanisms trigger at runtime, and enforcing the expected constraint in code stops all 38 exploits.

In summary, this paper makes the following contributions:

  • The first large-scale empirical study, to our knowledge, of how agentic systems specify and enforce the logic they delegate to LLM calls.

  • A contract view of LLM calls and a blind, multi-agent reconstruction method that measures missing constraints against a reference independent of the code.

  • A catalog of ten recurring gap patterns and concrete implications for agent developers, framework maintainers, and researchers.

  • A validated dataset of 496 call sites, 917 typed outputs, 5,017 implemented and 2,699 expected constraints, with source evidence and review records.

References (26)