JOB-4: LLM cost & latency observability + spend guardrails#61
Open
Pragadeesh122 wants to merge 2 commits into
Open
JOB-4: LLM cost & latency observability + spend guardrails#61Pragadeesh122 wants to merge 2 commits into
Pragadeesh122 wants to merge 2 commits into
Conversation
…anels
JOB-4: make LLM spend and latency visible and bounded on the existing
Prometheus/Grafana/OTel stack.
- Add per-session token ceiling guardrail (api/session_budget.py): Redis-backed
cumulative prompt+completion token counter per session, enforced at the top of
both general and project chat turns. Refuses turns once MAX_SESSION_TOKENS
(default 2,000,000) is exceeded; fails open on Redis errors. Trips counted in
agenticrag_session_budget_blocked_total{chat_type,limit}.
- Add agenticrag_retrieval_duration_seconds histogram (cache_status label) for
RAG retrieval p50/p95; emitted from pipeline/retriever.py.
- Grafana: add "RAG Retrieval Latency (p50/p95)" and "Chat/Stream Completion
Latency (p50/p95) by Mode" panels to UX & Latency dashboard, and "Session
Spend Guardrail Blocks" panel to Economics dashboard.
- Docs: document both guardrail layers (per-turn context cap + per-session
ceiling), defaults, and a spend/latency baseline section with read queries.
- Tests: tests/test_session_budget.py covers budget check/record/enforce,
fail-open, and the new metric emitters.
Co-Authored-By: Paperclip <[email protected]>
Pragadeesh122
force-pushed
the
job-4-llm-cost-latency-guardrails
branch
from
July 17, 2026 02:16
9fc2d79 to
95eb9d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
JOB-4 — make LLM spend and latency visible and bounded on the existing Prometheus/Grafana/OTel stack. Most per-request token/cost tracking and latency dashboards already existed; this PR closes the remaining acceptance gaps: RAG retrieval latency percentiles, a per-session spend guardrail, and a documented baseline.
Changes
Spend guardrail (new) —
api/session_budget.pyapi/chat.py) and project (api/project_chat.py) chat turns. OnceMAX_SESSION_TOKENS(default 2,000,000, env-tunable,0disables) is exceeded, the turn is refused with anerrorSSE +budget_exceededdoneevent.MAX_PROMPT_TOKENS→ summarization), which bounds a single request; this bounds a whole session.Latency visibility
agenticrag_retrieval_duration_secondshistogram (cache_status= hit/miss), emitted frompipeline/retriever.py— gives p50/p95 for RAG retrieval.agenticrag_session_budget_blocked_total{chat_type,limit}counter for guardrail trips.Grafana
Docs —
docs/architecture/observability.mdAcceptance criteria
Security / isolation note
The guardrail reads/writes only a hashed-key Redis counter and never logs raw session content; it fails open so it can't break chat. No auth/permission/RAG-isolation surfaces are touched.
Testing
tests/test_session_budget.py(10 tests): budget check/record/enforce, fail-open, disabled mode, and metric emitters.test_session_budget test_llm_stream_metrics test_retrieval_cache test_chat_orchestration test_observability_context→ 33 passed.🤖 Generated with Claude Code