Skip to content

Feature/rate limit retry #54

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jiange91
Copy link
Contributor

To get around API provider rate limit, we currently queue all outgoing requests by model type and dispatch them with rate control and retry.

  1. Cognify will start a local server to buffer all litellm requests, port can be set by -p or --rate_limit_port.
  2. The server will maintain per-model rate limit and complete requests accordingly.
  3. The rate limit will be automatically fetched according to the first response for each model.
  4. Added a simple heuristic to adjust the rate limit:
    • If raise rate limit error, reduce ticket generate speed by half.
    • Otherwise, for each successful response, increase the current rate limit by 1.
  5. Throttled requests will be added to the end of queue and retry.

rate_limit_pool[req.model] += 1
except RateLimitError as e:
# reduce rate limit by half and put to the back of the queue
rate_limit_pool[req.model] /= 2

Choose a reason for hiding this comment

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

What about refactor this into a backoff function that a user can control the strategy? "/2" may be too aggressive.

semaphore.release()

# --- Worker Thread Function ---
def worker(semaphore, task_queue):

Choose a reason for hiding this comment

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

worker is single process multi-thread. I'm not sure if it will becomes the performance bottleneck. Can we write a short piece of code to test its performance limit?

result = {"result": {**response.model_dump(), "_hidden_params": response._hidden_params, "_response_headers": response._response_headers}}
job_results[job_id] = result
# increase rate limit by 1
rate_limit_pool[req.model] += 1

Choose a reason for hiding this comment

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

This should also refactor into a separate function and let user decide the strategy. In some cases we don't need to increase the rate limit.

except RateLimitError as e:
# reduce rate limit by half and put to the back of the queue
rate_limit_pool[req.model] /= 2
task_queue.put((job_id, req))

Choose a reason for hiding this comment

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

This assumes the requester won't timeout. It is probably ok for cognify but not necessary ok for a generic rate limiter. Can you add a comment?

**model_kwargs
)

# response = completion(

Choose a reason for hiding this comment

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

If the client does not want rate limiter (e.g., to ease the debugging to avoid another http endpoint, or in the replay mode), can they revert to the non-rate limiter functionality?

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.

2 participants