Skip to content

epic: Session Lifetime Cap Plugin #708

Closed
3 of 3 issues completed
Closed

Description

@evaline-ju

Problem

Autonomous agents have no platform-enforced ceiling on token spend per session. A runaway or unexpectedly deep run exhausts quota silently with no cost visibility and no circuit breaker. Session lifetime budgets give platform operators a transparent guardrail — enforced at the sidecar layer with no agent code changes.

This evolves two existing implementations: llm-budget-proxy (standalone proxy, per-session token caps via sync Postgres, requires agents to route explicitly) and litellm-budget-track (same sidecar plugin slot, but daily USD ceiling from a local file with no cross-pod state).

Why a Cortex plugin

Cortex already sits on every inference call as part of the pod's auth and policy pipeline — it sees token counts in LLM responses without any agent cooperation. It also tracks per-session state, so budget counters are naturally scoped to each agent session without additional plumbing. Enforcing budgets here means uniform coverage across agent frameworks, no per-agent instrumentation, and workload-level isolation. The alternative — a standalone proxy or per-framework integration — requires agents to opt in and breaks when they don't.

User Interaction

An operator sets per-agent limits in sidecar config:

- name: token-budget
  config:
    max_tokens: 50000
    max_calls: 100
    max_duration_seconds: 1800
    on_exceed: deny   # or "observe" for shadow mode

When a session crosses a limit, the next inference request gets a 429 updated to 403:

{"error": "budget_exceeded", "reason": "token limit reached", "spent": 45000, "limit": 50000}

User Stories

  1. As a platform operator, I can see how many tokens each agent session is consuming — so I can identify expensive sessions and establish baselines before setting limits
  2. As a platform operator, I can set budget thresholds in shadow mode and see which sessions would have been denied — so I can calibrate limits from real traffic without risk
  3. As a platform operator, I can enforce a hard token/call/time ceiling per session — so a runaway agent is stopped before it exhausts quota
  4. As a platform operator, I can query historical spend across sessions — so I can report on agent cost by team or workload over time

design

Implementation Breakdown

  1. Token count extraction — Wire inference-parser to emit per-session token totals to session API

  2. Storage abstraction — General-purpose plugin storage interface for cross-pod persistent state. Initial implementation backed by Redis, isolated as a separate module to avoid coupling.

    Operation Purpose
    Get / Set (with TTL) Caching (tokens, decisions)
    Incr Atomic counters (rate limiting)
    HashIncr / HashGet / HashSetNX Structured per-session state (budget counters)
    Expire TTL management for any key
  3. Counter accumulation — Async write path, key expiration, background refresh loop, fail-open behavior

  4. Budget evaluation engine — Check max_tokens, max_calls, max_duration_seconds against cached counters

  5. Shadow mode — Log would-have-denied events; no blocking

  6. Enforcement mode — Structured 429 response with spend/limit/reason

  7. Per-agent configuration — Plugin config in sidecar YAML, workload identity scoping

  8. Audit/reporting — Optional durable spend log for historical queries (storage TBD)

  9. Testing — Unit tests for evaluation logic; integration tests for pod restart, storage failure, multi-replica accuracy, and overshoot bounds

  10. Documentation — Plugin config reference, shadow→enforce rollout runbook, failure mode guide, orchestrator 429 handling guide

Open Questions

Storage scope — The interface covers token-budget and basic rate limiting. Are there other planned plugin use cases that would require expanding it (e.g. pub/sub for session fan-out, atomic composite operations)?

Connection sharing — Should plugins share a single storage connection (fewer connections, requires binary-level wiring) or each manage their own (simpler plugin code, more connections)? [Leaning toward independent per-plugin connections since plugins are generally self-contained lifecycles and connection overhead should be minimal.]

Success criteria

Users of the plugin can observe and enforce bounds on tokens, calls, and time per agent session

Stories

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. huang195 commented on Jul 28, 2026

    @huang195
    Member

    Stopping a long running agent due to exceeded budget seems excessive if it's still making reasonable progress. I'm wondering if HITL would be a possible alternative to just denying additional inference calls? As Cortex could potentially hook onto HITL events, we could have another plugin that decides if it is worth letting the agent continue by granting it a temporary budget increase.

  2. esnible commented on Jul 29, 2026

    @esnible
    Member

    429s generally include a Retry-After header, because at some point the client is expected to work again.

    If this is a true lifetime cap, perhaps the error should be 503 Service Unavailable?

    We should use 429 for daily and hourly caps.

  3. evaline-ju commented on Jul 30, 2026

    @evaline-ju
    ContributorAuthor

    Thanks for the feedback! Good points

    As Cortex could potentially hook onto HITL events, we could have another plugin that decides if it is worth letting the agent continue by granting it a temporary budget increase.

    Fair, an error on a productive agent might not be desirable - instead of a separate plugin, we could potentially extend this plugin to add an action for on_exceed where instead of only deny or observe, we have human or hitl and hand this off to a HITL interaction/hook. We could also directly refine the scope here if this would be a more compelling direct story.

    429s generally include a Retry-After header, because at some point the client is expected to work again.
    If this is a true lifetime cap, perhaps the error should be 503 Service Unavailable?

    Fair on the retry. Perhaps 403 Forbidden could be appropriate? 503 seems to imply the server is broken

  4. evaline-ju commented on Aug 12, 2026

    @evaline-ju
    ContributorAuthor

    Basic functionality was added in #723 - as noted in the PR some future potential directions, unprioritized are extending with: OTel metrics, HITL, and general session identity identification across harnesses

  5. self-assigned this
    on Aug 12, 2026
  6. evaline-ju commented on Aug 19, 2026

    @evaline-ju
    ContributorAuthor

    Refactored tracking a bit - #778 is created after to track the original #723 PR work for observe + deny

  7. moved this from Done to In progress in Rossoctl Issue Prioritizationon Aug 19, 2026
  8. evaline-ju commented on Sep 9, 2026

    @evaline-ju
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions