agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Feature]: Support proactive per-model rate limiting (e.g. max requests per minute with blocking wait)

Open 75ACOL opened this issue 8 months ago • 4 comments

Feature Description

Add proactive per-model rate limiting support for Java AgentScope, so callers can configure a request budget such as "max 20 requests per minute" and the SDK will wait before sending the next model request instead of only failing after the provider starts throttling.

This is different from retry-after-failure. The need here is client-side throttling before requests are sent.

Motivation

In multi-agent workflows, one top-level agent may trigger several sub-agents and model calls in a short time window. Even when each individual call is valid, the aggregate request rate can exceed the provider capacity and produce errors like:

  • Too many requests. Your requests are being throttled due to system capacity limits.
  • HTTP 429
  • provider-specific 500/503 responses that actually mean throttling or temporary overload

AgentScope Java already exposes:

  • ExecutionConfig.maxAttempts(...)
  • ExecutionConfig.initialBackoff(...)
  • ExecutionConfig.maxBackoff(...)
  • ReActAgent.Builder.maxIters(...)

These are useful, but they do not solve the proactive throttling use case.

Proposed API Shape

One possible design is to support rate limiting in model execution config or model transport config, for example:

ExecutionConfig.builder()
    .maxAttempts(3)
    .initialBackoff(Duration.ofSeconds(2))
    .maxBackoff(Duration.ofSeconds(30))
    .maxRequestsPerMinute(20)
    .build();

Or a dedicated rate-limit config object:

RateLimitConfig.builder()
    .maxRequests(20)
    .per(Duration.ofMinutes(1))
    .burst(5)
    .waitMode(BLOCK)
    .build();

And then:

ReActAgent.builder()
    .model(model)
    .modelExecutionConfig(modelExecutionConfig)
    .build();

Expected Behavior

  • Support per-model or per-agent model request throttling
  • Support blocking/waiting when the budget is exhausted
  • Work for both non-streaming and streaming model calls
  • Be shared across concurrent sub-agent calls when they use the same underlying model/client
  • Keep retry/backoff behavior as a separate concern

Why This Matters

Without proactive rate limiting, users of multi-agent systems have to implement their own global throttling layer outside AgentScope. That is possible, but awkward because the throttling concern belongs very close to model execution.

Built-in support would make AgentScope Java more reliable for provider backends with strict RPM/QPM limits, especially in orchestrated workflows.

Additional Context

75ACOL avatar Mar 16 '26 01:03 75ACOL

If this is used in a client-side scenario, waiting in place may be acceptable. But in a server-side scenario, the pressure this puts on the system probably needs more evaluation. Especially when the caller is using synchronous blocking calls, requests may keep piling up while waiting for quota to recover, which can amplify resource pressure on both the server side and the caller side, and potentially drag both down. The default behavior and the intended usage boundary here may need a bit more design discussion.

uuuyuqi avatar Apr 11 '26 14:04 uuuyuqi

The underlying issue here isn’t just missing rate limiting — it’s how pressure is handled.

This proposal introduces:

maxRequestsPerMinute + blocking wait

That works in client-side scenarios, but in multi-agent/server contexts it can amplify the failure mode.

When aggregate request rate exceeds provider capacity, this is typically a CAP condition (throughput/concurrency), not just a WAIT condition (time-based backoff).

Blocking wait assumes:

  • pressure will resolve with time

But in orchestrated workflows:

  • requests accumulate while waiting
  • pressure shifts from provider → your system
  • when quota frees, you release a burst and recreate the same limit

This leads to:

  • queue amplification
  • false liveness (system appears active but is stalled)
  • resource pressure on threads / connections

A more robust model may need to distinguish:

  • WAIT → short-lived throttling → delay is appropriate
  • CAP → sustained throughput pressure → reduce parallelism or shed load

So instead of only:

waitMode(BLOCK)

SirBrenton avatar Apr 11 '26 15:04 SirBrenton

I see two distinct requirements for rate limiting here:

Provider-side limits: To avoid triggering rate limits from model vendors, we can move the throttling logic upstream into the AgentScope framework. Given the distributed nature of the deployment, this will necessitate cluster-wide rate limiting capabilities.

System stability: To protect the application's stability, concurrency control (managing the number of in-flight requests) would be more effective than traditional QPS or QPM metrics.

LearningGp avatar Apr 13 '26 12:04 LearningGp

Hi maintainers, I’d like to work on this issue.

I’ve reviewed the current model execution flow and would like to confirm two design details before implementation:

  1. When multiple agents use the same underlying model/client, should the rate-limit state be owned by the model instance, the transport/client, or an explicitly shared limiter?
  2. Should retry attempts acquire a new permit, or should a permit cover the complete logical request including its retries?

Once these semantics are confirmed, I plan to keep the feature opt-in, preserve the existing timeout/retry behavior, and cover streaming, non-streaming, and concurrent calls with deterministic tests.

goodCircle avatar Aug 17 '26 17:08 goodCircle