Skip to content

Commit 140693b

Browse files
committed
fix(agent): account for an explicit maxTokens output demand in the budget estimate (issue #137 review)
1 parent b5ad401 commit 140693b

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
* the request carries an explicit tool selection, only the selected bindings are
4141
* counted; the dynamic bindings of the request are always counted because they are
4242
* disclosed next to the selected ones.
43+
*
44+
* <p>
45+
* When the request carries an explicit {@code maxTokens} output limit, the estimate
46+
* accounts for that demand instead of the configured output reserve alone: the gate
47+
* must refuse a request whose explicitly requested output does not fit into the
48+
* window, even when the input itself is small.
4349
*/
4450
public final class ApproximateRequestTokenEstimator implements RequestTokenEstimator {
4551

@@ -73,7 +79,19 @@ public RequestTokenEstimate estimate(ModelRequest request, ContextWindowBudget b
7379
Objects.requireNonNull(request, "request must not be null");
7480
Objects.requireNonNull(budget, "budget must not be null");
7581
return new RequestTokenEstimate(systemTokens(request), messageTokens(request), toolSchemaTokens(request),
76-
budget.outputReserve(), EstimationAccuracy.APPROXIMATE);
82+
outputDemand(request, budget), EstimationAccuracy.APPROXIMATE);
83+
}
84+
85+
/**
86+
* The output demand of the request: an explicit {@code maxTokens} option counts as
87+
* the demanded output, floored by the configured output reserve.
88+
*/
89+
private int outputDemand(ModelRequest request, ContextWindowBudget budget) {
90+
Integer maxTokens = request.getOptions() == null ? null : request.getOptions().maxTokens();
91+
if (maxTokens == null) {
92+
return budget.outputReserve();
93+
}
94+
return Math.max(budget.outputReserve(), maxTokens);
7795
}
7896

7997
private int systemTokens(ModelRequest request) {

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.github.agentic.ai.agent.interceptor.ModelRequest;
2121
import io.github.agentic.ai.agent.interceptor.ModelResponse;
2222
import io.github.agentic.ai.api.chat.ChatMessage;
23+
import io.github.agentic.ai.api.chat.GenerationOptions;
2324
import io.github.agentic.ai.api.chat.ToolCall;
2425
import io.github.agentic.ai.api.chat.ToolDefinition;
2526
import io.github.agentic.ai.api.tool.Tool;
@@ -58,6 +59,71 @@ void theEstimateCoversSystemMessagesToolSchemasAndTheOutputReserve() {
5859
assertThat(estimate.accuracy()).isEqualTo(EstimationAccuracy.APPROXIMATE);
5960
}
6061

62+
// issue #137 review: an explicit maxTokens output limit is an output demand —
63+
// a request whose requested completion alone exceeds the window must be refused
64+
// even when its input is small.
65+
@Test
66+
void anExplicitOutputDemandBeyondTheWindowIsRefused() {
67+
AtomicInteger dispatches = new AtomicInteger();
68+
ContextBudgetInterceptor interceptor = ContextBudgetInterceptor.builder()
69+
.budget(new ContextWindowBudget(1000, 100))
70+
.build();
71+
ModelRequest demanded = ModelRequest.builder()
72+
.systemMessage(ChatMessage.system("system instructions for the agent"))
73+
.messages(List.of(ChatMessage.user("hello")))
74+
.options(GenerationOptions.builder().maxTokens(2000).build())
75+
.build();
76+
77+
assertThatThrownBy(() -> interceptor.interceptModel(demanded, r -> {
78+
dispatches.incrementAndGet();
79+
return response();
80+
})).isInstanceOf(ContextWindowBudgetExceededException.class)
81+
.satisfies(failure -> {
82+
ContextWindowBudgetExceededException exceeded = (ContextWindowBudgetExceededException) failure;
83+
assertThat(exceeded.dominantComponent()).isEqualTo(BudgetComponent.OUTPUT_RESERVE);
84+
});
85+
86+
assertThat(dispatches).hasValue(0);
87+
}
88+
89+
// The same gate must not reject a request whose explicit output limit fits.
90+
@Test
91+
void anExplicitOutputDemandWithinTheWindowStillDispatches() {
92+
AtomicInteger dispatches = new AtomicInteger();
93+
ContextBudgetInterceptor interceptor = ContextBudgetInterceptor.builder()
94+
.budget(new ContextWindowBudget(1000, 100))
95+
.build();
96+
ModelRequest modest = ModelRequest.builder()
97+
.systemMessage(ChatMessage.system("system instructions for the agent"))
98+
.messages(List.of(ChatMessage.user("hello")))
99+
.options(GenerationOptions.builder().maxTokens(50).build())
100+
.build();
101+
102+
interceptor.interceptModel(modest, r -> {
103+
dispatches.incrementAndGet();
104+
return response();
105+
});
106+
107+
assertThat(dispatches).hasValue(1);
108+
}
109+
110+
// The estimate itself accounts for the explicit output demand, floored by the
111+
// configured reserve.
112+
@Test
113+
void theEstimateAccountsForAnExplicitMaxTokens() {
114+
RequestTokenEstimate estimate = new ApproximateRequestTokenEstimator().estimate(request(), budget());
115+
116+
ModelRequest demanded = ModelRequest.builder()
117+
.messages(List.of(ChatMessage.user("hello")))
118+
.options(GenerationOptions.builder().maxTokens(2000).build())
119+
.build();
120+
RequestTokenEstimate demandedEstimate = new ApproximateRequestTokenEstimator().estimate(demanded, budget());
121+
122+
assertThat(estimate.outputReserve()).isEqualTo(100);
123+
assertThat(demandedEstimate.outputReserve()).isEqualTo(2000);
124+
assertThat(demandedEstimate.totalTokens()).isEqualTo(demandedEstimate.inputTokens() + 2000);
125+
}
126+
61127
// issue #137 review: the budget must hold after interceptor rewrites, so the
62128
// budget interceptor has to sit **after** every request-rewriting interceptor
63129
// in the chain (innermost). This chain-level case registers [rewriter, budget]:

0 commit comments

Comments
 (0)