Skip to content

Commit 7e1033b

Browse files
committed
fix(agent): estimate the tool schemas the dispatch actually sends (issue #137 review)
1 parent 140693b commit 7e1033b

2 files changed

Lines changed: 93 additions & 12 deletions

File tree

‎argi-agent/argi-agent-api/src/main/java/io/github/agentic/ai/agent/budget/ApproximateRequestTokenEstimator.java‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
* disclosed tool schemas and the reserved output.
3737
*
3838
* <p>
39-
* The tool schemas are counted for the bindings the request actually discloses. When
40-
* the request carries an explicit tool selection, only the selected bindings are
41-
* counted; the dynamic bindings of the request are always counted because they are
42-
* disclosed next to the selected ones.
39+
* The tool schemas are counted for the bindings the request actually dispatches,
40+
* matching the selection semantics of the dispatch path: the static and the dynamic
41+
* registry are merged with the static binding winning a name conflict, and an
42+
* explicit tool selection (including an empty one, which disables tools) filters the
43+
* merged set. Schemas of bindings the dispatch will not send are not counted, so an
44+
* empty selection or a large dynamic catalog cannot reject a request whose dispatch
45+
* carries none of them. Tool descriptions are not counted: they feed the auxiliary
46+
* selection call, never the dispatched request.
4347
*
4448
* <p>
4549
* When the request carries an explicit {@code maxTokens} output limit, the estimate
@@ -124,18 +128,22 @@ else if (part.data() != null) {
124128
}
125129

126130
private int toolSchemaTokens(ModelRequest request) {
127-
Set<String> selection = request.hasToolSelection() ? new HashSet<>(request.getTools()) : null;
128-
int total = 0;
131+
// Merge both registries with the static binding winning a name conflict, then
132+
// apply the explicit selection — the same semantics as the dispatch path.
133+
Map<String, ToolDefinition> disclosed = new java.util.LinkedHashMap<>();
129134
for (ToolDefinition definition : request.getToolRegistry().definitions()) {
130-
if (selection == null || selection.contains(definition.name())) {
131-
total += definitionTokens(definition);
132-
}
135+
disclosed.putIfAbsent(definition.name(), definition);
133136
}
134137
for (ToolDefinition definition : request.getDynamicToolRegistry().definitions()) {
135-
total += definitionTokens(definition);
138+
disclosed.putIfAbsent(definition.name(), definition);
136139
}
137-
for (Map.Entry<String, String> description : request.getToolDescriptions().entrySet()) {
138-
total += tokens(description.getKey()) + tokens(description.getValue());
140+
if (request.hasToolSelection()) {
141+
Set<String> selection = new HashSet<>(request.getTools());
142+
disclosed.values().removeIf(definition -> !selection.contains(definition.name()));
143+
}
144+
int total = 0;
145+
for (ToolDefinition definition : disclosed.values()) {
146+
total += definitionTokens(definition);
139147
}
140148
return total;
141149
}

‎argi-agent/argi-agent-api/src/test/java/io/github/agentic/ai/agent/budget/ContextBudgetInterceptorTest.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,79 @@ void theEstimateAccountsForAnExplicitMaxTokens() {
124124
assertThat(demandedEstimate.totalTokens()).isEqualTo(demandedEstimate.inputTokens() + 2000);
125125
}
126126

127+
// issue #137 review: the tool schemas must match what the dispatch actually sends.
128+
// The empty explicit selection disables tools, so a large dynamic catalog must not
129+
// count against the request.
130+
@Test
131+
void anEmptyToolSelectionDoesNotCountDynamicSchemas() {
132+
AtomicInteger dispatches = new AtomicInteger();
133+
ContextBudgetInterceptor interceptor = ContextBudgetInterceptor.builder()
134+
.budget(new ContextWindowBudget(1000, 100))
135+
.build();
136+
ModelRequest disabled = ModelRequest.builder()
137+
.systemMessage(ChatMessage.system("system instructions for the agent"))
138+
.messages(List.of(ChatMessage.user("hello")))
139+
.toolRegistry(new ToolRegistry(List.of()))
140+
.dynamicToolRegistry(new ToolRegistry(List.of(tool("catalog", "{\"type\":\"object\",\"properties\":{"
141+
+ "field1\":{\"type\":\"string\"},field2:{\"type\":\"string\"}}}"))))
142+
.tools(List.of())
143+
.build();
144+
RequestTokenEstimate estimate = new ApproximateRequestTokenEstimator().estimate(disabled, budget());
145+
assertThat(estimate.toolSchemaTokens()).isZero();
146+
147+
interceptor.interceptModel(disabled, r -> {
148+
dispatches.incrementAndGet();
149+
return response();
150+
});
151+
assertThat(dispatches).hasValue(1);
152+
}
153+
154+
// An explicit selection filters the dynamic registry exactly like the dispatch does.
155+
@Test
156+
void anExplicitSelectionFiltersDynamicSchemas() {
157+
ModelRequest selected = ModelRequest.builder()
158+
.systemMessage(ChatMessage.system("system instructions for the agent"))
159+
.messages(List.of(ChatMessage.user("hello")))
160+
.toolRegistry(new ToolRegistry(List.of(tool("lookup", "{\"type\":\"object\"}"))))
161+
.dynamicToolRegistry(new ToolRegistry(List.of(tool("catalog", "{\"type\":\"object\",\"properties\":{"
162+
+ "field1\":{\"type\":\"string\"},field2:{\"type\":\"string\"}}}"))))
163+
.tools(List.of("lookup"))
164+
.build();
165+
RequestTokenEstimate estimate = new ApproximateRequestTokenEstimator().estimate(selected, budget());
166+
167+
ModelRequest staticOnly = ModelRequest.builder()
168+
.systemMessage(ChatMessage.system("system instructions for the agent"))
169+
.messages(List.of(ChatMessage.user("hello")))
170+
.toolRegistry(new ToolRegistry(List.of(tool("lookup", "{\"type\":\"object\"}"))))
171+
.build();
172+
RequestTokenEstimate staticEstimate = new ApproximateRequestTokenEstimator().estimate(staticOnly, budget());
173+
174+
assertThat(estimate.toolSchemaTokens()).isEqualTo(staticEstimate.toolSchemaTokens());
175+
}
176+
177+
// A dynamic binding shadowed by a static name is dispatched once — the schema is
178+
// counted once, not twice.
179+
@Test
180+
void aDynamicSchemaShadowedByAStaticNameIsCountedOnce() {
181+
ModelRequest shadowed = ModelRequest.builder()
182+
.systemMessage(ChatMessage.system("system instructions for the agent"))
183+
.messages(List.of(ChatMessage.user("hello")))
184+
.toolRegistry(new ToolRegistry(List.of(tool("lookup", "{\"type\":\"object\"}"))))
185+
.dynamicToolRegistry(new ToolRegistry(List.of(tool("lookup", "{\"type\":\"object\",\"properties\":{"
186+
+ "field1\":{\"type\":\"string\"},field2:{\"type\":\"string\"}}}"))))
187+
.build();
188+
RequestTokenEstimate estimate = new ApproximateRequestTokenEstimator().estimate(shadowed, budget());
189+
190+
ModelRequest staticOnly = ModelRequest.builder()
191+
.systemMessage(ChatMessage.system("system instructions for the agent"))
192+
.messages(List.of(ChatMessage.user("hello")))
193+
.toolRegistry(new ToolRegistry(List.of(tool("lookup", "{\"type\":\"object\"}"))))
194+
.build();
195+
RequestTokenEstimate staticEstimate = new ApproximateRequestTokenEstimator().estimate(staticOnly, budget());
196+
197+
assertThat(estimate.toolSchemaTokens()).isEqualTo(staticEstimate.toolSchemaTokens());
198+
}
199+
127200
// issue #137 review: the budget must hold after interceptor rewrites, so the
128201
// budget interceptor has to sit **after** every request-rewriting interceptor
129202
// in the chain (innermost). This chain-level case registers [rewriter, budget]:

0 commit comments

Comments
 (0)