|
20 | 20 | import io.github.agentic.ai.agent.interceptor.ModelRequest; |
21 | 21 | import io.github.agentic.ai.agent.interceptor.ModelResponse; |
22 | 22 | import io.github.agentic.ai.api.chat.ChatMessage; |
| 23 | +import io.github.agentic.ai.api.chat.GenerationOptions; |
23 | 24 | import io.github.agentic.ai.api.chat.ToolCall; |
24 | 25 | import io.github.agentic.ai.api.chat.ToolDefinition; |
25 | 26 | import io.github.agentic.ai.api.tool.Tool; |
@@ -58,6 +59,71 @@ void theEstimateCoversSystemMessagesToolSchemasAndTheOutputReserve() { |
58 | 59 | assertThat(estimate.accuracy()).isEqualTo(EstimationAccuracy.APPROXIMATE); |
59 | 60 | } |
60 | 61 |
|
| 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 | + |
61 | 127 | // issue #137 review: the budget must hold after interceptor rewrites, so the |
62 | 128 | // budget interceptor has to sit **after** every request-rewriting interceptor |
63 | 129 | // in the chain (innermost). This chain-level case registers [rewriter, budget]: |
|
0 commit comments