agentleFS
htlin222htlin222/openevidence-mcp/.claude/skills/openevidence-rate-limits/SKILL.md

openevidence-rate-limits

Skill83 starsChanged 4 days agoApache-2.0View on GitHub

Implement OpenEvidence rate limiting, backoff, and request optimization. Use when handling rate limit errors, implementing retry logic, or optimizing API request throughput for clinical queries. Trigger with phrases like "openevidence rate limit", "openevidence throttling", "openevidence 429", "openevidence retry", "openevidence backoff".

The file

Files
SKILL.md

OpenEvidence Rate Limits

Self-harnessing rate limiter for the OpenEvidence cookie API. Tuned so the client never bursts above its own configured RPM, even before the server returns a 429.

Overview

OpenEvidence does not publish a stable X-RateLimit-* contract for the cookie-based web API. This skill ships a defense-in-depth limiter that:

  1. Sliding-window self-throttle caps requests-per-window at a configurable target usage (default 80% of maxRequestsPerWindow).
  2. Hard burst cap prevents tight loops from firing more than burstCap requests in any 1-second slice.
  3. Priority queue — stat > urgent > routine > research — so a STAT clinical query jumps ahead of a research batch.
  4. Exponential backoff with jitter on 408/425/429/5xx, with Retry-After honored verbatim when the server sends it.
  5. Opportunistic header parsing — if X-RateLimit-Remaining / X-RateLimit-Reset ever appear, they tighten the limiter; otherwise we rely on the client-side counters.

The limiter is fully testable: clock and sleeper are injectable so the test suite runs in ~1 second.

Implementation map

FileRole
src/rate-limit.tsRateLimitController, withExponentialBackoff, computeBackoffDelay, parseRetryAfterMs, HttpError.
src/openevidence-client.tsrequestWithRateLimit wraps every fetch with acquire/observe/release + retry.
src/config.tsresolveRateLimitConfig reads env vars and folds them into AppConfig.rateLimit.
src/types.tsOpenEvidenceAskRequest.priority?: ClinicalPriority.
test/rate-limit.test.ts25 tests including a 50-concurrent burst integration test.

Configuration (env vars)

VariableDefaultEffect
OE_MCP_RPM60Max requests per window (typically 1 min).
OE_MCP_RATE_WINDOW_MS60000Sliding-window length.
OE_MCP_BURST6Hard cap per 1 s slice.
OE_MCP_RATE_TARGET80Throttle when usage hits this % of RPM.
OE_MCP_MAX_CONCURRENT4In-flight request cap.
OE_MCP_MAX_RETRIES5Total retries on 429/5xx.
OE_MCP_RETRY_BASE_MS1000Backoff base — base * 2^attempt.
OE_MCP_RETRY_MAX_MS60000Backoff ceiling.
OE_MCP_RETRY_JITTER_MS500Uniform jitter added to backoff.

Conservative production preset (single small practice):

export OE_MCP_RPM=30
export OE_MCP_BURST=3
export OE_MCP_RATE_TARGET=70
export OE_MCP_MAX_CONCURRENT=2

Using priorities from MCP tool calls

The oe_ask tool accepts an optional priority — set it when wiring clinical contexts:

{
  "name": "oe_ask",
  "arguments": {
    "question": "Contraindications for tPA in acute ischemic stroke?",
    "priority": "stat"
  }
}

stat and urgent requests bypass any queued routine / research acquirers but still respect the sliding window — they cannot burst past the configured RPM.

Inspecting state

const client = new OpenEvidenceClient(resolveConfig());
await client.init();
// ...issue a few requests
console.log(client.getRateLimitMetrics());
// {
//   requestsInWindow: 12, usagePercent: 20, headroom: 48,
//   active: 1, queued: { stat: 0, urgent: 0, routine: 0, research: 0 },
//   serverRemaining: null, serverResetAt: null, retryAfterUntil: null
// }

Surface this via a /health/openevidence endpoint if running behind HTTP.

Algorithm cheat sheet

acquire(priority):
  if higher-priority queues non-empty OR active >= maxConcurrent:
    park in queues[priority]            # FIFO within priority
  loop:
    wait = max(
      retryAfterUntilMs - now,           # honor server Retry-After
      serverResetMs - now (if remaining=0),
      burstCap window relief,            # <= burstCap per 1s
      sliding window relief,             # <= targetUsage% of RPM
    )
    if wait <= 0: break
    sleep(wait)
  record now in requestTimes

release() hands the concurrency slot directly to the next priority waiter — slots are never momentarily free if work is pending, which avoids a thundering herd.

Testing

pnpm run test runs all suites (citations + rate-limit). Notable cases:

  • withExponentialBackoff: retries on 429 then succeeds — happy retry path.
  • withExponentialBackoff: surfaces non-retryable 4xx immediately — 403 short-circuits without burning attempts.
  • RateLimitController: maxConcurrent + priority ordering — proves a released slot goes to stat even when routine queued first.
  • RateLimitController.observe: Retry-After header blocks acquire — server-driven backoff wins over local counters.
  • integration: self-harness never exceeds RPM under burst — 50 concurrent acquires; assert no 1-second window exceeds the cap.

Failure modes & remedies

SymptomLikely causeFix
Many 429s despite throttleOE tightened capLower OE_MCP_RPM / OE_MCP_RATE_TARGET.
Long stalls on oe_askA previous 429 with no Retry-After set a 2× base backoff.Wait, or shrink OE_MCP_RETRY_BASE_MS.
stat requests still queuedConcurrency saturated by long-running routine.Bump OE_MCP_MAX_CONCURRENT or move routine to research.
Tests hangReal setTimeout instead of injected clock.Pass the fakeClock from test/rate-limit.test.ts.

Related

  • Cookie-based auth lives in src/cookies.ts — not affected by this skill.
  • Crossref retries are independent and still use their own simple backoff.
  • For a portable (Python stdlib) variant of the MCP itself, see openevidence-skill/SKILL.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.