Repository navigation
Python: docs(python): add concurrency-safe per-tool rate limiting middleware sample - #9785
Open
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Open
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Suhas Aggarwal (suhasagg) wants to merge 2 commits into
Conversation
Suhas Aggarwal (suhasagg)
requested review from
SergeyMenshykh and
Eduard van Valkenburg (eavanvalkenburg)
as code owners
October 28, 2026 07:36
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 28, 2026 07:36 — with
GitHub Actions
Inactive
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 28, 2026 07:36 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Sampling time before acquiring the lock can admit calls beyond the configured burst, and normal CI commands do not collect the 17 new regression tests.
2 open issues
What changed in this PR
Adds a sample-only approach to per-tool rate limiting through Python’s existing FunctionMiddleware extension point, following the direction of #9239 without changing core APIs.
Changes:
- Introduces per-tool token buckets with lock-protected accounting and rejection before tool execution.
- Adds 17 regression cases covering quotas, concurrency, cancellation, event-loop reuse, and middleware integration.
- Documents execution instructions, quota ownership, and in-process limitations.
| File | Description |
|---|---|
python/samples/02-agents/middleware/test_per_tool_rate_limiting.py |
Adds offline admission-control and integration tests. |
python/samples/02-agents/middleware/README.md |
Documents sample usage, rejection behavior, and limitations. |
python/samples/02-agents/middleware/per_tool_rate_limiting.py |
Implements token-bucket middleware and a standalone demonstration. |
Review effort: Balanced
Suhas Aggarwal (suhasagg)
temporarily deployed
to
github-app-auth
October 28, 2026 08:10 — with
GitHub Actions
Inactive
Suhas Aggarwal (suhasagg)
deployed
to
github-app-auth
October 28, 2026 10:20 — with
GitHub Actions
Active
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Motivation & Context
Issue #9239 discusses per-tool rate limiting and admission control for Python Agent Framework tool execution.
The maintainer recommended demonstrating this capability through the existing
FunctionMiddlewareextension point rather than introducing a new framework-level rate-limiting API.This PR follows that direction by providing a focused, opt-in sample showing how applications can enforce in-process, per-tool quotas before invoking tools.
The example is useful for agents interacting with external APIs, rate-limited services, or tools where excessive concurrent requests should be controlled.
Description & Review Guide
What are the major changes?
per_tool_rate_limiting.py, demonstrating token-bucket admission control throughFunctionMiddleware.time.monotonic()for refill accounting andthreading.Lockfor concurrency-safe token updates.call_next(), preventing downstream tool execution.test_per_tool_rate_limiting.pywith 17 tests covering policy validation, quota accounting, refill, concurrent admission, cancellation, event-loop reuse, and framework middleware integration.What is the impact of these changes?
This is a sample-only contribution. It does not modify core framework behavior, public APIs, or existing middleware contracts.
The sample demonstrates application-local admission control and intentionally does not claim distributed enforcement, automatic retries, or exactly-once execution.
Rate-limit rejection is represented as a tool-visible result rather than using
MiddlewareTerminationto stop the entire tool loop.What do you want reviewers to focus on?
FunctionMiddlewareconventions.Related Issue
Fixes #9239.
This PR implements the sample-only direction discussed in the issue. It does not attempt to introduce the broader core API originally proposed.
This also replaces the approach in the previously closed PR #9293 by keeping all rate-limiting logic inside the samples directory, without adding public framework exports.
Local Validation
Executed locally using Python 3.10:
python -m pytest test_per_tool_rate_limiting.py -v— 17 passedruff check per_tool_rate_limiting.py test_per_tool_rate_limiting.py— passedruff format --check per_tool_rate_limiting.py test_per_tool_rate_limiting.py— passedpython per_tool_rate_limiting.py— passed, demonstrating two admitted calls followed by one rejected callAI Assistance
AI assistance details:
AI assistance was used for implementation, test development. I reviewed the generated changes and validated the implementation locally using pytest, Ruff, and sample execution.
Contribution Checklist