Skip to content

docs: Document thread-safe RateLimiter for multi-agent usage#255

Merged
MervinPraison merged 2 commits intomainfrom
claude/issue-252-20260424-1016
Apr 28, 2026
Merged

docs: Document thread-safe RateLimiter for multi-agent usage#255
MervinPraison merged 2 commits intomainfrom
claude/issue-252-20260424-1016

Conversation

@MervinPraison
Copy link
Copy Markdown
Owner

@MervinPraison MervinPraison commented Apr 24, 2026

Fixes #252

This PR implements the documentation requirements from issue #252 to document the thread-safe RateLimiter functionality for multi-agent and parallel usage scenarios.

Changes Made

Major Update: docs/features/rate-limiter.mdx

  • Complete rewrite following AGENTS.md structure template
  • Added hero Mermaid diagram showing shared rate limiter concept
  • 3-step Quick Start with Steps component
  • How It Works section with sequence diagram and explanation table
  • Choose Your Mode decision diagram for different use cases
  • New Thread Safety & Multi-Agent Use section with code patterns
  • Best Practices with AccordionGroup
  • Configuration table with all SDK parameters

Minor Update: docs/features/thread-safety.mdx

  • Added RateLimiter entries to Lock Types table
  • Added Rate Limiter subsection under Thread-Safe Components with cross-link

Quality Verification

  • SDK Accuracy: All configuration options match actual source code
  • Code Examples: All imports are correct and examples are copy-paste runnable
  • AGENTS.md Compliance: Uses standard structure, Mermaid colors, and Mintlify components
  • Beginner-Friendly: Non-developers can understand the approach
  • Thread Safety Documentation: Properly explains the behavioral guarantee changes

Generated with Claude Code

Summary by CodeRabbit

  • Documentation
    • Enhanced rate-limiter documentation with visual diagrams showing architecture and request flow
    • Clarified how rate limiting works as a shared budget model across agents and threads
    • Added comprehensive configuration guidance for rate-limiting setup
    • Expanded thread-safety documentation with detailed concurrency guarantees and practical usage examples for multi-threaded environments

)

- Rewrite docs/features/rate-limiter.mdx with comprehensive thread safety content
- Add hero diagram, 3-step Quick Start, sequence + mode-selection diagrams
- Include Thread Safety & Multi-Agent Use section with code patterns
- Add lock types table entry and cross-link in docs/features/thread-safety.mdx
- Follow AGENTS.md structure with Steps, AccordionGroup, CardGroup components
- Use standard Mermaid color scheme (#8B0000, #F59E0B, #10B981, #6366F1)
- All code examples use correct imports and are copy-paste runnable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 24, 2026 10:20
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d4cb044e-e367-44d3-8ac8-aace363affa8

📥 Commits

Reviewing files that changed from the base of the PR and between 93aeaf8 and 844a158.

📒 Files selected for processing (2)
  • docs/features/rate-limiter.mdx
  • docs/features/thread-safety.mdx

📝 Walkthrough

Walkthrough

Documentation updates to rate limiter features defining shared RPM/TPM budget architecture across agents and threads. Includes expanded configuration guidance, thread-safety guarantees, code examples for concurrent usage, and architectural diagrams. Parallel update to thread-safety documentation adds RateLimiter lock-type mappings.

Changes

Cohort / File(s) Summary
Rate Limiter Documentation
docs/features/rate-limiter.mdx
Comprehensive rewrite redefining rate limiting as shared budget across agents/threads. Adds architecture and request-flow Mermaid diagrams, expands configuration section with requests_per_minute, tokens_per_minute, and burst parameters. Extends concurrency section with thread-safety guarantees, multi-thread execution patterns, and available_tokens/available_api_tokens monitoring examples. Adds manual acquire patterns (sync/async/non-blocking) and best-practice guidance. Updates frontmatter with sidebar metadata and icon.
Thread-Safety Documentation
docs/features/thread-safety.mdx
Adds "Rate Limiter" section to lock-type matrix documenting sync (threading.Lock) and async (asyncio.Lock) locking mechanisms for token/refill state management during concurrent acquire calls.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 A limiter shared, across threads it leaps,
With locks standing guard through the concurrent deep,
Token buckets dance in the async night,
While RPMs bloom in the rate-limiting light! ✨

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/issue-252-20260424-1016

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances the Rate Limiter documentation with new diagrams, usage examples, and configuration details, while also updating the thread-safety guide. Feedback indicates that the documentation makes incorrect claims regarding the thread-safety of synchronous operations, as the underlying implementation reportedly lacks the necessary locking mechanisms. These factual errors in the documentation should be corrected to accurately reflect the code's behavior.

| Step | What happens |
|------|--------------|
| Refill | Tokens regenerate based on elapsed time and `requests_per_minute` / `tokens_per_minute`. |
| Acquire | A thread reserves a token; under contention, only one thread mutates state at a time. |
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The description for the 'Acquire' step states that 'under contention, only one thread mutates state at a time'. This is currently inaccurate for synchronous calls. The implementation of RateLimiter.acquire() in praisonaiagents/llm/rate_limiter.py does not use any threading.Lock, meaning concurrent calls from multiple threads will result in race conditions when updating internal state like _tokens and _last_update.


<Note>
The standalone `rate_limiter=` parameter is deprecated. Use `execution=ExecutionConfig(rate_limiter=obj)` instead.
Every method on `RateLimiter` — both sync (`acquire`, `acquire_tokens`, `try_acquire`, `reset`) and async (`acquire_async`, `acquire_tokens_async`) — is safe to call concurrently. You can share a single `RateLimiter` across threads, `AgentTeam` members, `PraisonAIAgents`, and `ParallelToolCallExecutor` workers without exceeding the configured budget.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This section claims that synchronous methods such as acquire and reset are safe to call concurrently. However, the source code for RateLimiter lacks threading.Lock protection for these methods. Sharing a single instance across threads (e.g., using ThreadPoolExecutor as shown in the example below) is not thread-safe for synchronous operations in the current implementation.

```

<Note>
`available_tokens` and `available_api_tokens` are safe to read from any thread — they acquire the same locks as `acquire()` internally.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The assertion that available_tokens and available_api_tokens acquire locks internally is incorrect. Neither these properties nor the synchronous acquire() method implement locking in the provided source code. This makes monitoring the budget from multiple threads unsafe as it triggers state mutations via _refill() without synchronization.


### Rate Limiter

`RateLimiter` can be shared across threads and agents. Both the sync and async method families are fully locked — see [Rate Limiter → Thread Safety & Multi-Agent Use](/docs/features/rate-limiter#thread-safety--multi-agent-use) for patterns.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The documentation states that synchronous method families of RateLimiter are 'fully locked'. However, the implementation in praisonaiagents/llm/rate_limiter.py does not use locks for synchronous methods, only for the lazy initialization of async locks. This statement is misleading for users expecting thread safety in multi-threaded environments.

|-----------|-----------|--------|
| chat_history | `Lock` | Simple mutual exclusion |
| caches | `RLock` | Allows reentrant access |
| `RateLimiter` (sync) | `threading.Lock` | Protects `_tokens`, `_api_tokens`, and refill state from races in multi-threaded acquire calls |
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The table entry for RateLimiter (sync) incorrectly identifies threading.Lock as the protection mechanism for internal state. In reality, the implementation does not use this lock during acquisition calls, leaving the state vulnerable to races in multi-threaded contexts. The documentation should be corrected to reflect the actual implementation or the implementation should be updated to match these claims.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

…0424-1016

# Conflicts:
#	docs/features/rate-limiter.mdx
@MervinPraison MervinPraison merged commit 33983ba into main Apr 28, 2026
@MervinPraison MervinPraison deleted the claude/issue-252-20260424-1016 branch April 28, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: Document thread-safe RateLimiter for multi-agent / parallel usage (PR #1537)

2 participants