Repository navigation
feat(llm): policy layer + cost ledger — per-job routing, privacy classes, fallback chains - #19
Merged
Merged
Conversation
…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>
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
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 toanthropic,openai(any OpenAI-compatible endpoint) orollamagenerate.jobs.<job>:provider,model,mode(realtime | batch — recorded, not yet executed),privacy,fallbackgenerate.prices_usd_per_mtok: extends the built-in Anthropic list pricesdecide.*: the typed-decision pins moved here fromdecide.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 asrefused: …with the one-line fix;ollamabecomes its own provider onOLLAMA_HOST; model precedence explicit > per-task env > policy job > provider default, with a fit check so aclaude-*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 forpubliconly untilengram 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_callrow — job, provider, model, mode, tokens in/out (cache tokens too),cost_usdfrom the price table (local = $0), latency, outcomeok | unavailable | refused.engram roigains an LLM spend section by job and model;engram llm-status/memory_llm_statusshow 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_KEYis unset —ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN,ant authprofiles, 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,roioutput, CLIollamaas its own providerdocs/COMMANDS.mdregenerated🤖 Generated with Claude Code