Skip to content

JOB-4: LLM cost & latency observability + spend guardrails#61

Open
Pragadeesh122 wants to merge 2 commits into
mainfrom
job-4-llm-cost-latency-guardrails
Open

JOB-4: LLM cost & latency observability + spend guardrails#61
Pragadeesh122 wants to merge 2 commits into
mainfrom
job-4-llm-cost-latency-guardrails

Conversation

@Pragadeesh122

Copy link
Copy Markdown
Owner

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.py

  • Redis-backed cumulative prompt+completion token counter per chat session (shares the 24h session TTL).
  • Enforced at the top of both general (api/chat.py) and project (api/project_chat.py) chat turns. Once MAX_SESSION_TOKENS (default 2,000,000, env-tunable, 0 disables) is exceeded, the turn is refused with an error SSE + budget_exceeded done event.
  • Fails open on any Redis error — an observability guardrail must never take down the chat path.
  • Complements the existing per-turn context cap (MAX_PROMPT_TOKENS → summarization), which bounds a single request; this bounds a whole session.

Latency visibility

  • New agenticrag_retrieval_duration_seconds histogram (cache_status = hit/miss), emitted from pipeline/retriever.py — gives p50/p95 for RAG retrieval.
  • New agenticrag_session_budget_blocked_total{chat_type,limit} counter for guardrail trips.

Grafana

  • UX & Latency: RAG Retrieval Latency (p50/p95) + Chat/Stream Completion Latency (p50/p95) by Mode.
  • Economics: Session Spend Guardrail Blocks.

Docsdocs/architecture/observability.md

  • New Spend Guardrails section (both layers, defaults, env vars) and a Baseline section with copy-paste PromQL for cost/1K-tokens and p50/p95 latency, plus a bounded worst-case cost model.

Acceptance criteria

  • Per-request token + cost by model and chat mode — Economics dashboard (pre-existing) documented.
  • p50/p95 latency for chat/stream and RAG retrieval — added retrieval histogram + p50/p95 panels.
  • A basic guardrail with sane defaults, documented — per-session token ceiling + existing context caps, all documented.
  • Baseline noted — methodology, read queries, and bounded cost model in docs.

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.
  • Full related suite green: test_session_budget test_llm_stream_metrics test_retrieval_cache test_chat_orchestration test_observability_context33 passed.

🤖 Generated with Claude Code

Pragadeesh122 and others added 2 commits July 16, 2026 21:16
…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
Pragadeesh122 force-pushed the job-4-llm-cost-latency-guardrails branch from 9fc2d79 to 95eb9d2 Compare July 17, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant