The AI Council team shared how they cut context costs for an AI agent by 2x without sacrificing code quality. The source experiment on a Keycloak project showed: an agent with access to the team’s collective knowledge wrote code that passed code review more easily, but overshot by 30 % due to token volume. After reworking the knowledge delivery architecture, cost dropped 2x, while code acceptance metrics stayed at previous levels.
Related: agent memory CLAUDE.md
Related: PRD method for AI agents
What is AI Council and why it’s needed
AI Council is an architectural pattern giving an agent access to accumulated project knowledge: decisions, style agreements, refactoring history, naming rules, and typical errors. Unlike classic RAG, where vector search retrieves relevant doc fragments, Council works as structured memory: the agent addresses concrete artifacts (ADRs, conventions, check-lists) as needed during a task.
The problem this approach solves is “context explosion.” When connecting a full repo or full documentation to every agent query, token cost grows linearly, and answer quality isn’t guaranteed to improve. Council proposes an alternative: a curated knowledge base the agent treats as long-term project memory.
Keycloak experiment: numbers and findings
Test polygon — a real open-source Keycloak project (Java, 200+ K lines). Two agent scenarios on identical tasks (bug fixes, feature additions, module refactoring):
- Baseline — agent without Council, only current file context and task instructions.
- Council v1 — agent with full access to project knowledge base (~150 artifacts: ADRs, style guide, migration guides, security notes).
First run results:
| Metric | Baseline | Council v1 |
|---|---|---|
| Avg task cost (tokens) | 1.0x (base) | 1.3x (+30 %) |
| Code acceptance rate on review | 42 % | 61 % |
| Iterations to acceptance | 2.8 | 1.6 |
| Dev time on review (min) | 18 | 9 |
Conclusion: Council v1 delivers quality code, but pays for it with tokens. At production load +30 % per task — unacceptable.
How they made it 2x cheaper: Council v2 architecture
Key idea — shift knowledge delivery from “everything at once” mode to “on-demand with caching” mode. Implementation in three stages:
1. Artifact hierarchy with priorities
All knowledge split into three tiers:
- Core (always-on) — 15 to 20 critical rules: architectural principles, security baseline, forbidden patterns. Loaded into agent’s system prompt continuously (~800 tokens).
- Contextual (on-demand) — modular guides: “how to add new SPI”, “migration to WildFly 32”, “working with Offline Tokens”. Loaded only when relevance detected for the task (classifier on embeddings + keyword heuristics).
- Reference (lookup) — full ADR texts, historical decisions, detailed posts in team blog. Available via tool-calling (council.lookup(artifact_id)), agent invokes them explicitly when context isn’t enough.
2. Session cache with TTL
Within a single agent work session (typically 3-7 tasks in a row) loaded Contextual artifacts are cached. Repeated accesses to same knowledge domains don’t require repeated token transfer. TTL — 30 minutes of inactivity or large context switch (switching between auth / admin / themes modules).
3. Adaptive history compression
Instead of full agent dialog with Council, a summarized version is kept: “task X — used artifacts A, B, C; result — accepted.” On next similar task agent gets only links to relevant artifacts, not the full reasoning log.
Council v2 results
| Metric | Baseline | Council v1 | Council v2 |
|---|---|---|---|
| Task cost (tokens) | 1.0x | 1.3x | 0.5x (-50 %) |
| Code accepted without fixes | 42 % | 61 % | 59 % |
| Iterations to acceptance | 2.8 | 1.6 | 1.7 |
| Dev review time (min) | 18 | 9 | 10 |
Cost dropped below baseline by 2x, code quality remained at Council v1 level (minus 2 pp — within noise). Main win — eliminating transfer of entire knowledge base to every task.
Project memory architecture: how it’s built inside
Council v2 stores knowledge as artifact graph with metadata:
{
"artifact_id": "adr-042-spi-loading",
"type": "adr",
"title": "Dynamic SPI Provider Loading",
"domain": ["spi", "extension-points", "classloading"],
"priority": "contextual",
"tokens": 1240,
"embedding": [...],
"dependencies": ["adr-015-module-system", "guide-spi-authoring"],
"last_updated": "2026-10-15",
"usage_count": 34
}Relevance classifier works in two passes: fast (BM25 on keywords + domain tags) filters 80 % of artifacts, precise (cosine similarity of task and artifact embeddings) ranks the rest. Tool-calling threshold — top-3 with similarity > 0.72.
Agent tool-calling interface:
// Automatic call on domain detection council.get_contextual(domain: "spi") // Explicit agent call when context insufficient council.lookup(artifact_id: "adr-042-spi-loading") // Session cache refresh council.refresh_cache(domains: ["spi", "security"])
Practical takeaways for adoption
1. Start with Core tier. 15 to 20 rules that prevent bugs or security issues give 80 % effect for 5 % tokens. Don’t wait for full knowledge base.
2. Invest in domain classifier. Quality domain tags on artifacts matter for embeddings — they work deterministically and cheaper.
3. Economy secret — session cache. Most tasks in a developer’s day fall in 2-3 domains. Cache eliminates repeated transfer of those same 2-3 thousand tokens.
4. Don’t replace Reference with RAG. Explicit tool-calling council.lookup() gives agent control and auditability: logs show which artifact was requested and why.
5. Make “tokens per accepted task” a KPI. Without it, context optimization turns into academic exercise.
What’s next: AI Council team plans
In backlog — cross-project Council (shared knowledge base for microservice architecture), automatic artifact extraction from PR comments and rétrospectives, integration with linters for real-time knowledge validation. Also experimenting with “obratnaya storona”: agent proposes new Council artifacts based on patterns it sees in code, but doesn’t find in base.
Full architecture breakdown of agent memory with config examples — in our article agent memory in project. PRD-method for agents (PRD-method) discussed in PRD-method for AI agents. Multi-agent team practice reviewed in multi-agent teams in practice.
Useful resources: LangGraph documentation, Hugging Face documentation.
Cheatsheet: agent memory architecture and token economy
Practical checklist for introducing Council approach in your project:
1. Extract 15 to 20 Core rules (security, architecture, taboos) — this is the system prompt, ~800 tokens.
2. Split remaining knowledge into Contextual (modular guides, 1-2k tokens each) and Reference (full ADRs, histories).
3. Tag every Contextual artifact with domain tags (spi, security, migration, testing) — this is the cheapest filter for embeddings.
4. Implement session cache with 30 min TTL — removes repeated transfer of same domains during a work day.
5. Give agent tool-calling council.lookup(artifact_id) for explicit Reference access — logs become auditable.
6. Track “tokens per accepted task” as core KPI: target -50 % vs baseline at preserved code acceptance %.
7. Configure classifier: BM25 + domain tags for 80 % filter, cosine similarity (threshold 0.• for top-3 ranking.
Example memory config (YAML for Council v2):
memory:
core_tokens_limit: 1000
contextual:
max_per_task: 3
token_budget: 4000
cache_ttl_minutes: 30
reference:
tool_name: council.lookup
max_calls_per_task: 5
classifier:
bm25_weight: 0.4
embedding_weight: 0.6
similarity_threshold: 0.72
Council v2 checklist: • Core layer (15 to 20 rules, ~800 tokens). • Contextual guides (1-2k tokens, domain tags). • Reference ADR (full texts). • Session cache TTL 30 min. • Tool council.lookup(artifact_id). • KPI: tokens/accepted_task -50%. • Classifier: BM25 + domain tags (80% filter), cosine sim >0.72 top-3. Config YAML example included.

