-
Notifications
You must be signed in to change notification settings - Fork 383
Move limit middlewares from splunklib.ai.hooks to splunklib.ai.limits
#759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| # Copyright © 2011-2026 Splunk, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"): you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from time import monotonic | ||
| from typing import Any, override | ||
|
|
||
| from splunklib.ai.messages import AgentResponse | ||
| from splunklib.ai.middleware import ( | ||
| AgentMiddleware, | ||
| AgentMiddlewareHandler, | ||
| AgentRequest, | ||
| ModelMiddlewareHandler, | ||
| ModelRequest, | ||
| ModelResponse, | ||
| ) | ||
| from splunklib.ai.structured_output import StructuredOutputGenerationException | ||
|
|
||
| DEFAULT_TIMEOUT_SECONDS: float = 600.0 | ||
| DEFAULT_STEP_LIMIT: int = 100 | ||
| DEFAULT_TOKEN_LIMIT: int = 200_000 | ||
| DEFAULT_STRUCTURED_OUTPUT_RETRY_LIMIT: int = 3 | ||
|
|
||
|
|
||
| class AgentStopException(Exception): | ||
| """Custom exception to indicate conversation stopping conditions.""" | ||
|
|
||
|
|
||
| class TokenLimitExceededException(AgentStopException): | ||
| """Raised by `Agent.invoke`, when token limit exceeds""" | ||
|
|
||
| def __init__(self, token_limit: int) -> None: | ||
| super().__init__(f"Token limit of {token_limit} exceeded.") | ||
|
|
||
|
|
||
| class StepsLimitExceededException(AgentStopException): | ||
| """Raised by `Agent.invoke`, when steps limit exceeds""" | ||
|
|
||
| def __init__(self, steps_limit: int) -> None: | ||
| super().__init__(f"Steps limit of {steps_limit} exceeded.") | ||
|
|
||
|
|
||
| class TimeoutExceededException(AgentStopException): | ||
| """Raised by `Agent.invoke`, when timeout exceeds""" | ||
|
|
||
| def __init__(self, timeout_seconds: float) -> None: | ||
| super().__init__(f"Timed out after {timeout_seconds} seconds.") | ||
|
|
||
|
|
||
| class StructuredOutputRetryLimitExceededException(AgentStopException): | ||
| """Raised by `Agent.invoke`, when structured output retry limit exceeds""" | ||
|
|
||
| def __init__(self, retry_count: int) -> None: | ||
| super().__init__(f"Structured output retry limit of {retry_count} exceeded") | ||
|
|
||
|
|
||
| class TokenLimitMiddleware(AgentMiddleware): | ||
| """Stops agent execution when the token count of messages passed to the model exceeds the given limit.""" | ||
|
|
||
| _limit: int | ||
|
|
||
| def __init__(self, limit: int) -> None: | ||
| self._limit = limit | ||
|
|
||
| @override | ||
| async def model_middleware( | ||
| self, | ||
| request: ModelRequest, | ||
| handler: ModelMiddlewareHandler, | ||
| ) -> ModelResponse: | ||
| if request.state.token_count >= self._limit: | ||
| raise TokenLimitExceededException(token_limit=self._limit) | ||
| return await handler(request) | ||
|
|
||
|
|
||
| class StepLimitMiddleware(AgentMiddleware): | ||
| """Stops agent execution when the number of steps taken reaches the given limit.""" | ||
|
|
||
| _limit: int | ||
|
|
||
| def __init__(self, limit: int) -> None: | ||
| self._limit = limit | ||
|
|
||
| @override | ||
| async def model_middleware( | ||
| self, | ||
| request: ModelRequest, | ||
| handler: ModelMiddlewareHandler, | ||
| ) -> ModelResponse: | ||
| if request.state.total_steps >= self._limit: | ||
| raise StepsLimitExceededException(steps_limit=self._limit) | ||
| return await handler(request) | ||
|
|
||
|
|
||
| class TimeoutLimitMiddleware(AgentMiddleware): | ||
| """Stops agent execution when wall-clock time within an invoke exceeds the given seconds. | ||
|
|
||
| The deadline resets on every invoke call - it measures time from the start of | ||
| each invocation, not from agent construction. | ||
|
|
||
| Do not share instances between agents. | ||
| """ | ||
|
|
||
| _seconds: float | ||
| _deadline_per_thread_id: dict[str, float] | ||
|
|
||
| def __init__(self, seconds: float) -> None: | ||
| self._seconds = seconds | ||
| self._deadline_per_thread_id = {} | ||
|
|
||
| @override | ||
| async def agent_middleware( | ||
| self, | ||
| request: AgentRequest, | ||
| handler: AgentMiddlewareHandler, | ||
| ) -> AgentResponse[Any | None]: | ||
| try: | ||
| # Agent loop starting. | ||
| self._deadline_per_thread_id[request.thread_id] = ( | ||
| monotonic() + self._seconds | ||
| ) | ||
| return await handler(request) | ||
| finally: | ||
| del self._deadline_per_thread_id[request.thread_id] # don't leak memory | ||
|
|
||
| @override | ||
| async def model_middleware( | ||
| self, | ||
| request: ModelRequest, | ||
| handler: ModelMiddlewareHandler, | ||
| ) -> ModelResponse: | ||
| if monotonic() >= self._deadline_per_thread_id[request.state.thread_id]: | ||
| raise TimeoutExceededException(timeout_seconds=self._seconds) | ||
| return await handler(request) | ||
|
|
||
|
|
||
| class StructuredOutputRetryLimitMiddleware(AgentMiddleware): | ||
| """Stops agent execution when the agent exceeds structured output | ||
| retry limit during a single agent loop invocation. Pass 0 to disable retires. | ||
| """ | ||
|
|
||
| _limit: int | ||
| _retries_per_thread_id: dict[str, int] | ||
|
|
||
| def __init__(self, limit: int) -> None: | ||
| self._limit = limit | ||
| self._retries_per_thread_id = {} | ||
|
|
||
| @override | ||
| async def agent_middleware( | ||
| self, | ||
| request: AgentRequest, | ||
| handler: AgentMiddlewareHandler, | ||
| ) -> AgentResponse[Any | None]: | ||
| try: | ||
| # Agent loop starting. | ||
| self._retries_per_thread_id[request.thread_id] = 0 | ||
| return await handler(request) | ||
| finally: | ||
| del self._retries_per_thread_id[request.thread_id] # don't leak memory | ||
|
|
||
| @override | ||
| async def model_middleware( | ||
| self, | ||
| request: ModelRequest, | ||
| handler: ModelMiddlewareHandler, | ||
| ) -> ModelResponse: | ||
| try: | ||
| return await handler(request) | ||
| except StructuredOutputGenerationException: | ||
| self._retries_per_thread_id[request.state.thread_id] += 1 | ||
| if self._retries_per_thread_id[request.state.thread_id] > self._limit: | ||
| raise StructuredOutputRetryLimitExceededException(self._limit) | ||
| raise # re-raise, to retry structured output generation | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.