Skip to content

feat(llm): policy layer + cost ledger — per-job routing, privacy classes, fallback chains - #19

Merged
luiscota99 merged 1 commit into
mainfrom
feat/llm-policy-cost
Oct 16, 2026
Merged

luiscota99 merged 1 commit into
mainfrom
feat/llm-policy-cost

Conversation

@luiscota99

@luiscota99 luiscota99 commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

The hub spec's LLM node, rules first: a policy file maps each job to provider, model, mode, token budget, privacy class and fallback chain … every call is logged with job, model, tokens and cost into Engram's ROI reporting. This PR is that file and that ledger. A decision model may later choose within a job's allowed set (#17); it never routes outside it.

Policy file ~/.engram/policy.json (ENGRAM_POLICY_PATH), engram policy show [--jobs] | set <dotted.key> <value> | path:

  • generate.providers.<p>.privacy: which content classes may go to anthropic, openai (any OpenAI-compatible endpoint) or ollama
  • generate.jobs.<job>: provider, model, mode (realtime | batch — recorded, not yet executed), privacy, fallback
  • generate.prices_usd_per_mtok: extends the built-in Anthropic list prices
  • decide.*: the typed-decision pins moved here from decide.py (same API)

Routing (llm.resolve_job): provider chain tried in order; a provider not cleared for the job's privacy class is skipped and logged as refused: … with the one-line fix; ollama becomes its own provider on OLLAMA_HOST; model precedence explicit > per-task env > policy job > provider default, with a fit check so a claude-* name never reaches Ollama on a fallback hop. With no policy file the behaviour is the pre-policy one: one provider, no fallback.

Privacy rule, enforced (deliberate behaviour change): jobs default to personal; a remote OpenAI-compatible base URL (OpenRouter, DeepSeek, Gemini) is cleared for public only until engram policy set generate.providers.openai.privacy '["personal","public"]' or the job is marked public. Localhost endpoints (Ollama's /v1) are always cleared, so the default install is unchanged.

Cost ledger: every attempt writes an llm_call row — job, provider, model, mode, tokens in/out (cache tokens too), cost_usd from the price table (local = $0), latency, outcome ok | unavailable | refused. engram roi gains an LLM spend section by job and model; engram llm-status / memory_llm_status show the resolved plan per job. Usage is captured on all three backends.

Credentials: the Anthropic backend now also accepts the SDK's own sources when ENGRAM_LLM_API_KEY is unset — ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN, ant auth profiles, Workload Identity Federation.

Verification

  • tests/test_policy_llm.py (10): defaults/merge/set, cost estimate incl. cache pricing, default plan, privacy refusal and both ways to clear it, fallback chain with per-attempt ledger rows and the model-fit rule, Anthropic cost summarized by job/model, precedence, status, roi output, CLI
  • Existing LLM and decide suites updated for ollama as its own provider
  • Full suite 1530 passed, zero real LLM calls (the two slow files take the same two minutes on main — pre-existing embedding work, worth its own look)
  • ruff clean, pyright 0 errors, Cloak 0 errors, docs/COMMANDS.md regenerated

🤖 Generated with Claude Code

…ses, fallback chains

The hub spec's LLM node: a policy file maps each job to provider, model,
mode, privacy class and fallback chain, and every call is logged with
job, model, tokens and cost into ROI reporting.

- src/policy.py: ~/.engram/policy.json (ENGRAM_POLICY_PATH) with
  `generate` (providers' privacy clearances, per-job entries, price table)
  and `decide` (moved here from decide.py; same API re-exported); dotted
  get/set; estimate_cost_usd with Anthropic list prices built in
- src/llm.py: resolve_job() builds the provider chain for a job, skips
  providers not cleared for the job's privacy class (logged as
  `refused: …` with the one-line fix), tries the rest in order; `ollama`
  is its own provider (OLLAMA_HOST) beside `anthropic` and the
  OpenAI-compatible `openai`; model precedence explicit > per-task env >
  policy job > provider default, with a fit check so a claude-* name
  never reaches Ollama on a fallback hop; every attempt → _ledger()
- cost ledger: search_audit.append_llm_call_audit (source llm_call: job,
  provider, model, mode, tokens incl. cache, cost_usd, latency, outcome);
  summarize_audit_log gains an `llm` section; `engram roi` prints "LLM
  spend" by job and model; get_llm_status exposes the resolved plan
- usage capture on all backends: llm_anthropic.generate_ex/chat_ex/
  complete_ex return (text, usage); OpenAI-compatible `usage`; Ollama
  prompt_eval_count/eval_count. Anthropic credentials: ENGRAM_LLM_API_KEY
  or the SDK's own sources (ANTHROPIC_API_KEY/AUTH_TOKEN, `ant auth`
  profiles, Workload Identity Federation)
- CLI: engram policy show [--jobs] | set <dotted.key> <value> | path
- behaviour change, deliberate: a REMOTE OpenAI-compatible base URL
  (OpenRouter, DeepSeek, Gemini) is cleared for `public` content only
  until the policy clears it — jobs default to `personal`; localhost
  endpoints are always cleared, so the default install is unchanged

Tests: tests/test_policy_llm.py (defaults/merge/set, cost estimate,
default plan, privacy refusal + both ways to clear it, fallback chain
with per-attempt ledger rows and the model-fit rule, Anthropic cost
summarized by job/model, precedence, status, roi output, CLI); existing
LLM/decide suites updated for `ollama` as a provider. Full suite 1531
passed with zero real LLM calls (the two slow files take the same two
minutes on main — pre-existing embedding work). ruff clean, pyright 0
errors, Cloak 0 errors, docs/COMMANDS.md regenerated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@luiscota99
luiscota99 merged commit 12f85bd into main Oct 16, 2026
5 checks passed
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