Skip to content

Priority and per-user round robin

Middleman accepts x-middleman-priority: low | medium | high, ignoring case and surrounding whitespace. A missing or unrecognized header means MEDIUM. Hawk evaluations default to MEDIUM and scans to LOW.

For an explicitly enabled, verified shared quota, Middleman uses strict priority with round robin between authenticated users:

  1. Wait for an opening in the group's configured max_inflight window.
  2. Choose HIGH before MEDIUM before LOW.
  3. Take the oldest queued request from the next user in that class.
  4. Start its upstream attempt and rotate that user to the back if they have more queued requests.

All of a user's jobs share their identity, with separate queues for their different priorities. Newly active users join the back. There are no token estimates, deficits, accumulated credits, or penalties for past usage. When every attempt costs one, ordinary round robin supplies the equal turns that deficit round robin would provide.

An internal retry consumes another turn. Equal attempts do not guarantee equal successful responses or token consumption: providers can reject differently sized requests, and consistently larger requests use more tokens. Compare completed requests and useful tokens separately. Sustained HIGH traffic may starve MEDIUM and LOW; that is the chosen priority policy. Callers are trusted to choose priorities cooperatively.

Openings, retries, and cancellation

max_inflight bounds attempts waiting for upstream response headers or settlement, not accepted generations. Accepted streams continue outside the window. A new HIGH request can displace an unsent lower-priority claim; calls already marked sending cannot be preempted. HIGH gets the next available slot, so a full window of slow lower-priority calls can still delay it.

Only definite capacity 429s retry internally. The adapter requires a complete, bounded error body and exhausted quota headers; billing, spending, acceleration, unknown errors, and uncertain transport failures return to the caller. An in-band error after HTTP 2xx never triggers an internal retry. Caller SDK retries remain independent.

A capacity refusal returns the request to its original FIFO position without rewinding the user ring. The class waits for valid retry-after-ms, then Retry-After, then the latest reset for exhausted quota dimensions. Missing guidance means one second; the minimum is 50 milliseconds. Concurrent refusals keep the latest deadline. Higher classes can interrupt that wait, while lower classes cannot. This also blocks lower traffic while retries await headers, even after the retry deadline; otherwise smaller lower-priority calls can repeatedly consume the refill that a refused request needs. Recovery ends when an accepted opening was claimed strictly after the latest cooldown deadline, or when the class becomes idle. An older success cannot erase a newer refusal. Same-millisecond ties remain protected until a later qualifying success or class idle; terminal or unknown outcomes do not prove recovery.

Cancelling queued or claimed work removes it without taking a turn. Cancelling an opening preserves its cancellation intent through settlement, so a subsequent 429 cannot resurrect it. Middleman never internally resends a request that might already have been accepted.

Configuration and quota routes

Administrators GET and PUT /admin/priority. PostgreSQL stores the complete configuration and assigns an increasing generation to each committed update; callers do not choose that generation. Workers refresh every five seconds, with cache notifications accelerating refresh. Valkey holds authoritative queue state shared across workers.

The default is {"mode":"off","groups":[]}. Enable scheduling explicitly on each selected group by adding:

"max_inflight": 4

This is an illustrative window. Choose a window from measured header latency and expected throughput, then test HIGH latency under a full window. Allowed values are 1–256; absent or null leaves the group on baseline routing. No token budgets, request-cost estimates, or algorithm versions are configured.

verified and quota_scope assert a checked quota identity. All aliases sharing the same provider quota must share one group. Its stable key is (provider, quota_scope), which must identify the relevant organization, project/workspace, and shared model family. Response headers and traffic observations cannot verify this identity. Leave unverified groups unverified until an operator checks their bindings.

Mode Behavior Route
off Baseline forwarding with route and usage observations Existing ordinary/HIGH credential
enforce, eligible opt-in Strict-priority round robin Explicit common-quota credential
No opt-in, unverified, or missing identity Labeled baseline fallback Existing ordinary/HIGH credential
Known eligible enforced group with stale configuration or unavailable shared state Retryable 429; no provider attempt No forwarding

For compatibility, shadow is accepted as an alias for off in API updates and stored configuration. Responses and new writes use off; neither value simulates scheduling.

After enqueue, baseline fallback requires confirmed removal and proof that sending never began. Otherwise a state error returns 429 without an internal resend. An unavailable or rotated common credential after selection likewise cannot cause a baseline resend.

For Anthropic, baseline_profile must match the model's anthropic_account; null preserves default-profile routing, falling back to static keys when no default profile is configured. common_profile names a configured credential profile reaching the shared quota without the ordinary workspace cap. Both ordinary and HIGH requests use its default credential during enforcement. Auth checks, public-model authorization, and upstream danger_name substitution remain unchanged.

For WIF baseline routing, each profile has its own workspace_id. Set high_priority_profile on the ordinary profile to a separate HIGH profile in the intended organization; omitting it preserves the single-workspace route.

The Anthropic benchmark is an ordinary workspace capped at 95%, while HIGH can use 100% of the parent quota. These are provider-side limits maintained in the Console, not a 95/5 hard split. Keep the baseline credentials and caps for rollback. New model families and increased parent quotas may require updating those absolute workspace caps.

For OpenAI models without openai_account, baseline_profile remains OPENAI_API_KEY or OPENAI_API_KEY_WITH_DATA_RETENTION, even when the ordinary route uses the default WIF profile. For models with openai_account, it must match that account exactly. common_profile accepts a configured OpenAI broker profile (WIF or API key), or an existing static key name from the lab's retention family. The static key names retain their meaning even if a broker profile has the same name.

OpenAI can use the same profile for ordinary and enforced traffic: for example, an account-less model can pair baseline_profile: "OPENAI_API_KEY" with common_profile: "prd-default". No separate credential is required for scheduling. Every priority class uses the common profile's ordinary credential, with tokens resolved after queue waits and again for each attempt; its high_priority_profile is not followed. Missing or failing named common profiles return errors without switching credentials. The off switch restores the existing ordinary/HIGH route.

Verify that the common credential reaches the intended shared quota and retention policy. Names alone do not establish project or organization identity, and other traffic sharing that quota can still bypass this queue. Compare against the actual OpenAI baseline; there is no assumed 95% workspace split.

The queue covers native Anthropic Messages and foreground OpenAI Chat Completions, Responses, and Decisions. Decisions retains its native JSON response and input-only billing. Token-counting, Responses compaction/background calls, and other providers retain baseline behavior. Caller-supplied Anthropic workspace and OpenAI organization/project headers cannot override these routes. Missing HIGH credentials retain the matching ordinary-credential fallback.

Bounds and configuration changes

Queue lifecycle commands run atomically in Valkey and return only their result and change sequence. This avoids competing read/modify/write retries and transferring the full queue on each operation. Descriptors, queues, and indexes are stored in separate fields so ordinary commands touch only the relevant records. One poller per worker and quota group wakes local waiters. At the existing 250 ms polling cadence, it also checks the presence of its parked descriptors so a displaced request wakes even if notifications were lost. Diagnostic snapshots explicitly read the full state. Load tests must include unrelated Valkey latency. A lost command reply is treated as uncertain, never blindly replayed.

New admission is bounded by min(max_inflight + 256, 1024) live descriptors per quota group, counting both waiting and opening requests. Internal capacity retries retain their reservations. At the limit, a higher-priority arrival replaces the newest waiting request of the lowest strictly lower priority; sending requests and equal-priority waiters cannot be displaced. A displaced caller receives a retryable 429. Existing queues above the admission limit drain naturally; higher-priority replacements can preserve their count, but new admissions cannot increase it.

The hard storage guards remain 1,024 live descriptors and 2 MiB of retained data, including hash fields and index entries; Valkey object overhead is additional. Diagnostic snapshots also have a 2 MiB serialized-response bound. Each worker retains at most 1,024 scheduled or unresolved requests; an uncertain settlement keeps its reservation until acknowledged. Workers reserve a 64 MiB body budget across queued/opening calls; Python object and response-buffer memory is additional. Queue residence, including internal retries, is at most 300 seconds. Full queues, body limits, expired waits, or uncertain state return provider-shaped 429 errors with a stable reason and no scheduler-generated Retry-After. SDKs retain their own backoff and jitter. Provider-origin responses and retry headers are unchanged; scheduler refusals are recorded separately from provider rate limits.

Workers heartbeat every second with a five-second lifetime. Unsent claims last at most two seconds; waiters poll at most every 250 ms. Each shared-state operation has a one-second budget, including waiting for a connection from the worker's bounded 100-connection pool. Pool saturation waits asynchronously instead of immediately reporting an outage. A crashed owner loses unsent work; its possibly accepted sends retire without replay. Losing the quota-binding registry requires admission off and all existing resource states drained before enforcement can reconstruct it. A partition or suspended process can outlive its ownership fence at the external provider, so this is not an external exactly-once guarantee.

A new configuration generation cancels unsent work and retains all old sending tokens until settlement or owner expiry. New enforcement waits behind those barriers. Switching off restores baseline intake immediately and leaves accepted streams running. A failed configuration refresh retains the last valid configuration without renewing its ten-second freshness. Once it expires, known eligible enforced groups return 429, including their unsent queued calls. Shared-state failures also return 429 before enqueue. A successfully loaded off setting takes effect even if shared-state publication fails. A stale worker may refuse until it can read that update; a worker with no successfully loaded configuration still starts off.

Moving aliases or group names to a different quota identity requires switching off and draining first. The admin endpoint validates this before committing, under a database lock shared across replicas. A publication failure after commit returns an explicit error; periodic refresh can recover publication. The off switch does not require working provider credentials or Valkey.

Validation and finite rollout

Traffic summaries record the selected route, generation, logical request ID, queue wait/outcome, attempt/refusal counts, and opening window. Existing usage and provider-header telemetry remains. upstream_latency_ms measures the final attempt through clean response-body completion and is absent for incomplete responses. It excludes queue waits and previous refused attempts; streaming duration can include client backpressure. Per-attempt header timings remain separate and include internally retried refusals. Telemetry contains no hypothetical admission decisions or estimated request costs.

priority_queue_removed means a queued descriptor disappeared; priority_queue_timeout means the local residence deadline elapsed. A successful pushout emits a best-effort priority.displaced event linking the incoming and victim logical request IDs. A lost enqueue reply can lose this event; no removal history is stored. Config and store failure events report phase, elapsed time, exception types, and worker context without request bodies or credentials. A shared dispatcher failure records the affected logical IDs once, so multiple refusals can be traced to one failed operation.

Before a trial, compare request completions and useful completed tokens with baseline, HIGH latency against a causal reference, and request-count fairness among continuously backlogged users. Include failures, cancellations, timeouts, client backlog, and pending work. Fewer 429s alone is not success. Exact round-robin attempts are an algorithm property; successful completion fairness requires measurement.

Existing off or shadow configurations from the previous allocator can remain in place during deployment; no configuration reset or SQL update is required. Parsing accepts and discards only these retired fields:

  • Top-level expected_workers, including null.
  • Per-group dimension, budget_per_minute, budget_source, prior_cost, input_cost, policy_version, and the prototype round_robin options.

Unknown fields and fields at the wrong level still fail validation. Group definitions and verification status are preserved; shadow becomes off. Legacy options never set max_inflight, so upgrading does not enable the new scheduler. If the old allocator is enforcing, switch it off before a rolling upgrade to avoid running mixed policies.

Reads leave stored JSON unchanged. After deployment, GET /admin/priority returns the current configuration shape, and the next PUT saves that shape without retired fields. Keep the original configuration if rollback to the old allocator is needed after a PUT; the old code requires the retired budget fields. The queue uses a fresh Valkey namespace and does not import the undeployed prototype format.

Wait until every replica runs the new code, then verify one whole quota group and its common route and select an experimentally supported window. Baseline observations in off can confirm telemetry, but cannot validate queue performance or quota identity.

Prepare a bounded 30-minute whole-group enforcement trial with no-HIGH saturation, HIGH bursts, expensive HIGH calls, and competing users. Watch absolute and avoidable HIGH latency, useful throughput, per-user progress, queue residence, retained bytes, state errors, and baseline fallbacks. Some throughput loss is acceptable for a simple policy, but quantify it explicitly.

Switch off immediately for a duplicate internal send, wrong credential route, lost queued work, unbounded memory, a stuck group, a broken off switch, or material HIGH regression. GET the current configuration, PUT it with mode: "off", and confirm baseline intake on every worker. Existing openings settle without replay; accepted streams continue. Preserve the baseline routes and caps throughout. Production activation requires separate authorization and a verified inventory.