DEV Community

Odd_Background_328
Odd_Background_328

Posted on

Turn DeepSeek V4's Peak/Off-Peak Pricing Into a Rate-Limit Contract, Not a Surprise

DeepSeek V4 introduced peak/off-peak pricing: API calls during high-demand hours cost more than off-peak calls. This is not a billing curiosity--it is a capacity signal that should change how your system routes requests. If your application blindly forwards every request to the same model at the same rate regardless of time, you will hit peak pricing and rate limits simultaneously.

What the pricing model changes

When a model provider charges different rates by time of day, two things happen:

  1. Your cost per request becomes time-dependent, breaking flat-rate budgeting
  2. Peak hours correlate with rate-limit pressure, meaning your most expensive requests are also the most likely to be throttled

A rate-limit contract that respects time windows

# rate_limit_contract.py
"""
Routes requests based on time-of-day pricing windows.
During peak hours, applies a stricter rate limit and queues non-urgent work.
"""
import time
from dataclasses import dataclass
from collections import deque

@dataclass
class PricingWindow:
    name: str
    start_hour: int  # UTC
    end_hour: int    # UTC
    rate_limit_per_min: int
    cost_per_million_tokens: float

PEAK = PricingWindow("peak", 8, 22, rate_limit_per_min=20, cost_per_million_tokens=4.0)
OFF_PEAK = PricingWindow("off-peak", 22, 8, rate_limit_per_min=100, cost_per_million_tokens=0.5)

def current_window(now_utc=None):
    now = now_utc or time.gmtime()
    hour = now.tm_hour
    if PEAK.start_hour <= hour < PEAK.end_hour:
        return PEAK
    return OFF_PEAK

class RateLimitedRouter:
    def __init__(self):
        self.timestamps = deque()
        self.queued = deque()

    def admit(self, request_id, urgent=False, now_utc=None):
        window = current_window(now_utc)

        # Evict old timestamps
        cutoff = time.time() - 60
        while self.timestamps and self.timestamps[0] < cutoff:
            self.timestamps.popleft()

        if len(self.timestamps) < window.rate_limit_per_min:
            self.timestamps.append(time.time())
            return {
                "action": "send",
                "window": window.name,
                "cost_estimate": window.cost_per_million_tokens,
                "request_id": request_id
            }

        if urgent:
            # Urgent requests bypass the queue but count against the limit
            self.timestamps.append(time.time())
            return {
                "action": "send_urgent",
                "window": window.name,
                "cost_estimate": window.cost_per_million_tokens * 1.5,  # Premium
                "request_id": request_id
            }

        # Queue non-urgent work for off-peak
        self.queued.append(request_id)
        return {
            "action": "queue",
            "window": window.name,
            "reason": f"Rate limit reached ({window.rate_limit_per_min}/min)",
            "request_id": request_id
        }

    def drain_queue(self, now_utc=None):
        """Move queued work to off-peak processing."""
        window = current_window(now_utc)
        if window.name != "off-peak":
            return []

        drained = []
        while self.queued and len(drained < window.rate_limit_per_min):
            drained.append(self.queued.popleft())
        return drained

if __name__ == "__main__":
    router = RateLimitedRouter()

    # Simulate 30 requests during peak
    results = []
    for i in range(30):
        result = router.admit(f"req-{i}", urgent=(i == 25))
        results.append(result)

    sent = sum(1 for r in results if r["action"].startswith("send"))
    queued = sum(1 for r in results if r["action"] == "queue")
    print(f"Peak test: {sent} sent, {queued} queued")
    assert queued > 0, "Should have queued excess requests"
    assert results[25]["action"] == "send_urgent", "Urgent should bypass"
    print("PASS: rate-limit contract enforced")
Enter fullscreen mode Exit fullscreen mode

What the contract enforces

Behavior Peak window Off-peak window
Rate limit 20 req/min 100 req/min
Cost per request $4.0/M tokens $0.5/M tokens
Non-urgent excess Queued Sent
Urgent excess Sent at premium Sent at standard

Monitoring fields

{
  "request_id": "req-001",
  "window": "peak",
  "action": "queue",
  "queue_depth": 4,
  "rate_limit_per_min": 20,
  "current_usage_per_min": 20,
  "cost_estimate_per_million": 4.0,
  "queued_at": "2026-07-20T14:30:00Z"
}
Enter fullscreen mode Exit fullscreen mode

Limitations

  • Pricing windows are provider-defined and may change without notice. Monitor the provider's pricing page and alert on schema changes.
  • The queue adds latency to non-urgent work. Set a maximum queue age before falling back to peak pricing.
  • This contract assumes a single provider. Multi-provider routing requires a separate cost-comparison layer.

What to check

Which of your current API requests are non-urgent and could be deferred to off-peak hours? If you cannot identify them, you are paying peak rates for background batch work.

Top comments (0)