Skip to content

Improve Mistral rate-limit handling and add cooldown probe scripts#70

Open
Android-PowerUser wants to merge 9 commits intocodex/analyze-technical-debt-and-complexity-o6bm9pfrom
fix-compilation-errors-in-mainactivity.kt-h8jilx
Open

Improve Mistral rate-limit handling and add cooldown probe scripts#70
Android-PowerUser wants to merge 9 commits intocodex/analyze-technical-debt-and-complexity-o6bm9pfrom
fix-compilation-errors-in-mainactivity.kt-h8jilx

Conversation

@Android-PowerUser
Copy link
Copy Markdown
Owner

Motivation

  • Make Mistral API usage more robust by adding better cooldown accounting, server-respected delays and adaptive backoff to avoid rapid retries and key contention.
  • Provide a standalone probe utility to empirically measure Mistral cooldown/lockout behavior for tuning client-side logic.

Description

  • Increase per-key minimum interval from 1100L to 1500L and add an overloaded markKeyCooldown that accepts an extraDelayMs to honor server-specified delays and adaptive backoff.
  • Parse Retry-After and x-ratelimit-reset headers via parseRetryAfterMs and parseRateLimitResetDelayMs, and compute an adaptive retry delay with adaptiveRetryDelayMs to escalate waits on repeated failures.
  • Raise maxAttempts and improve retry/error handling to mark keys as blocked and to throw clearer errors after exhausting attempts, plus ensure stopExecutionFlag is set/reset appropriately and add roundToLong import used by header parsing.
  • Add two scripts scripts/mistral_cooldown_probe.py and scripts/mistral_cooldown_probe.sh to probe cooldown timing by exercising streaming and non-streaming requests and measuring minimal successful wait intervals.

Testing

  • Ran the project build and checks with ./gradlew assembleDebug, ./gradlew test and ./gradlew lint, and they completed successfully.
  • Verified Kotlin code compiles and the added probe scripts are executable (python3 scripts/mistral_cooldown_probe.py wrapper present), and no automated tests failed during the change.

Codex Task

Copy link
Copy Markdown
Contributor

@amazon-q-developer amazon-q-developer bot left a comment

Choose a reason for hiding this comment

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

The PR improves Mistral API rate-limit handling with better cooldown logic and adaptive backoff. The Kotlin changes look solid - increased minimum interval to 1500ms, added header parsing for server-specified delays, and improved retry logic with adaptive backoff.

Critical security issue found in the probe script: hardcoded API key must be replaced with environment variable to prevent credential exposure in version control.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

import time
from typing import Tuple, List

MISTRAL_API_KEY = "zsEegAJFadHH4uooe2lW0HVNmy1rpqGT"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛑 Security Vulnerability: Hardcoded API key exposes credentials in version control1

Replace with environment variable to prevent credential leakage.

Suggested change
MISTRAL_API_KEY = "zsEegAJFadHH4uooe2lW0HVNmy1rpqGT"
MISTRAL_API_KEY = os.environ.get("MISTRAL_API_KEY", "")

Footnotes

  1. CWE-798: Use of Hard-coded Credentials - https://cwe.mitre.org/data/definitions/798.html

Comment on lines +2 to +4
import json
import subprocess
import time
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing import for os module required for environment variable access. This will cause a NameError at runtime when the suggested fix is applied.

Suggested change
import json
import subprocess
import time
import json
import os
import subprocess
import time

Android-PowerUser and others added 8 commits April 1, 2026 04:39
…tCoordinator.kt

Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com>
…or Mistral coordinator

Update callMistralApi to accept and use multiple API keys for proper rotation through MistralRequestCoordinator, instead of defeating the coordinator's purpose with a single-key list
…limit-issue

Introduce MistralRequestCoordinator and queueing for Mistral auto-screenshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant