sinapse-ai 9.3.0 → 9.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/CLAUDE.md +56 -343
- package/.claude/rules/agent-authority.md +6 -0
- package/.claude/rules/agent-handoff.md +5 -0
- package/.claude/rules/cross-squad-routing.md +5 -0
- package/.claude/rules/hook-governance.md +6 -0
- package/.claude/rules/mcp-usage.md +3 -1
- package/.claude/rules/safe-collaboration.md +10 -0
- package/.claude/rules/security-data-protection.md +9 -0
- package/.claude/rules/squad-awareness.md +3 -1
- package/.claude/rules/tool-examples.md +6 -0
- package/.claude/rules/workflow-execution.md +7 -0
- package/.codex/agents/analyst.md +253 -72
- package/.codex/agents/architect.md +455 -68
- package/.codex/agents/data-engineer.md +492 -106
- package/.codex/agents/developer.md +560 -0
- package/.codex/agents/devops.md +518 -69
- package/.codex/agents/product-lead.md +335 -0
- package/.codex/agents/project-lead.md +377 -0
- package/.codex/agents/quality-gate.md +449 -0
- package/.codex/agents/sinapse-orqx.md +9 -7
- package/.codex/agents/sprint-lead.md +287 -0
- package/.codex/agents/squad-creator.md +344 -0
- package/.codex/agents/ux-design-expert.md +495 -0
- package/.codex/delegation-matrix.json +756 -44
- package/.codex/handoff-packet.schema.json +30 -6
- package/.sinapse-ai/data/entity-registry.yaml +175 -363
- package/.sinapse-ai/data/registry-update-log.jsonl +16 -0
- package/.sinapse-ai/development/agents/analyst.md +90 -0
- package/.sinapse-ai/development/agents/architect.md +73 -0
- package/.sinapse-ai/development/agents/developer.md +69 -0
- package/.sinapse-ai/development/agents/devops.md +117 -0
- package/.sinapse-ai/development/agents/quality-gate.md +85 -0
- package/.sinapse-ai/development/checklists/agent-quality-gate.md +27 -0
- package/.sinapse-ai/development/checklists/brownfield-compatibility-checklist.md +20 -0
- package/.sinapse-ai/development/checklists/code-review-checklist.md +106 -0
- package/.sinapse-ai/development/checklists/issue-triage-checklist.md +9 -0
- package/.sinapse-ai/development/checklists/memory-audit-checklist.md +16 -0
- package/.sinapse-ai/development/checklists/pr-quality-checklist.md +72 -0
- package/.sinapse-ai/development/checklists/security-deployment-checklist.md +54 -0
- package/.sinapse-ai/development/checklists/self-critique-checklist.md +19 -1
- package/.sinapse-ai/development/skills/debug.md +57 -0
- package/.sinapse-ai/development/skills/fast-review.md +69 -0
- package/.sinapse-ai/development/skills/research-synthesis.md +77 -0
- package/.sinapse-ai/development/skills/security-scan.md +73 -0
- package/.sinapse-ai/development/skills/verify.md +53 -0
- package/.sinapse-ai/development/templates/squad/agent-template.md +17 -4
- package/.sinapse-ai/development/templates/squad/checklist-template.md +13 -5
- package/.sinapse-ai/development/templates/squad/task-template.md +7 -0
- package/.sinapse-ai/development/templates/squad/workflow-template.yaml +7 -0
- package/.sinapse-ai/development/workflows/fast-track.yaml +87 -0
- package/.sinapse-ai/infrastructure/scripts/validate-codex-delegation.js +3 -1
- package/.sinapse-ai/install-manifest.yaml +71 -35
- package/docs/codex-integration-process.md +22 -0
- package/docs/codex-parity-program.md +27 -0
- package/docs/ide-integration.md +36 -0
- package/package.json +1 -1
- package/squads/claude-code-mastery/knowledge-base/claude-code-internals-reference.md +927 -0
- package/squads/squad-brand/knowledge-base/archetype-brand-mapping.md +12 -1
- package/squads/squad-brand/knowledge-base/brand-activism-cultural-branding.md +216 -0
- package/squads/squad-brand/knowledge-base/brand-audit-criteria.md +58 -0
- package/squads/squad-brand/knowledge-base/brand-digital-strategy.md +188 -0
- package/squads/squad-brand/knowledge-base/brand-legal-ip.md +222 -0
- package/squads/squad-brand/knowledge-base/brand-naming-framework.md +163 -0
- package/squads/squad-brand/knowledge-base/branding-master-reference.md +1001 -0
- package/squads/squad-brand/knowledge-base/color-psychology.md +25 -12
- package/squads/squad-brand/knowledge-base/employer-personal-branding.md +206 -0
- package/squads/squad-brand/knowledge-base/routing-catalog.md +34 -0
- package/squads/squad-brand/knowledge-base/sonic-branding-principles.md +6 -1
- package/squads/squad-brand/knowledge-base/typography-personality.md +34 -0
- package/squads/squad-claude/knowledge-base/context-window-optimization.md +334 -0
- package/squads/squad-claude/knowledge-base/knowledge-architecture-reference.md +403 -0
- package/squads/squad-claude/knowledge-base/memory-systems-reference.md +412 -0
- package/squads/squad-claude/knowledge-base/obsidian-claude-integration.md +423 -0
- package/squads/squad-claude/knowledge-base/retrieval-augmented-generation.md +320 -0
- package/squads/squad-claude/knowledge-base/skill-creation-patterns.md +380 -0
- package/squads/squad-claude/knowledge-base/swarm-orchestration-patterns.md +411 -0
- package/squads/squad-cloning/knowledge-base/clone-quality-assurance.md +211 -0
- package/squads/squad-cloning/knowledge-base/confidence-scoring.md +51 -0
- package/squads/squad-cloning/knowledge-base/cross-squad-deployment.md +47 -0
- package/squads/squad-cloning/knowledge-base/ethical-guidelines.md +237 -0
- package/squads/squad-cloning/knowledge-base/knowledge-graph-for-clones.md +295 -0
- package/squads/squad-cloning/knowledge-base/memory-architecture-for-clones.md +229 -0
- package/squads/squad-cloning/knowledge-base/multi-agent-deployment-patterns.md +320 -0
- package/squads/squad-cloning/knowledge-base/skill-standard-for-clones.md +262 -0
- package/squads/squad-cloning/knowledge-base/sop-extraction-guide.md +243 -0
- package/squads/squad-commercial/knowledge-base/account-based-selling.md +206 -0
- package/squads/squad-commercial/knowledge-base/ai-as-competitive-infrastructure.md +14 -0
- package/squads/squad-commercial/knowledge-base/ai-in-sales.md +199 -0
- package/squads/squad-commercial/knowledge-base/brazilian-sales-context.md +195 -0
- package/squads/squad-commercial/knowledge-base/customer-success-operations.md +83 -2
- package/squads/squad-commercial/knowledge-base/prospecting-pipeline-generation.md +69 -0
- package/squads/squad-commercial/knowledge-base/sales-enablement-playbook.md +260 -0
- package/squads/squad-commercial/knowledge-base/sales-methodology-comparison.md +185 -0
- package/squads/squad-commercial/knowledge-base/sales-revenue-master-reference.md +1123 -0
- package/squads/squad-content/knowledge-base/brazilian-content-context.md +176 -0
- package/squads/squad-content/knowledge-base/competitor-analysis-methods.md +40 -1
- package/squads/squad-content/knowledge-base/content-architecture-taxonomy.md +206 -0
- package/squads/squad-content/knowledge-base/content-formats-encyclopedia.md +58 -1
- package/squads/squad-content/knowledge-base/content-references-bibliography.md +130 -0
- package/squads/squad-content/knowledge-base/content-strategy-master-reference.md +1097 -0
- package/squads/squad-content/knowledge-base/content-tech-stack.md +150 -0
- package/squads/squad-content/knowledge-base/copywriting-formulas-library.md +188 -0
- package/squads/squad-content/knowledge-base/email-newsletter-strategy.md +161 -0
- package/squads/squad-content/knowledge-base/platform-algorithm-intelligence.md +86 -1
- package/squads/squad-content/knowledge-base/social-algorithms-master-reference.md +1007 -0
- package/squads/squad-content/knowledge-base/video-audio-content-playbook.md +218 -0
- package/squads/squad-copy/knowledge-base/ai-copy-production.md +254 -0
- package/squads/squad-copy/knowledge-base/brazilian-copywriting-context.md +242 -0
- package/squads/squad-copy/knowledge-base/email-copywriting-system.md +299 -0
- package/squads/squad-copy/knowledge-base/landing-page-copy-architecture.md +267 -0
- package/squads/squad-copy/knowledge-base/power-words-catalog.md +205 -0
- package/squads/squad-copy/knowledge-base/seo-copywriting.md +255 -0
- package/squads/squad-copy/knowledge-base/video-script-copywriting.md +239 -0
- package/squads/squad-council/knowledge-base/brand-strategy-models.md +193 -0
- package/squads/squad-council/knowledge-base/growth-strategy-models.md +267 -0
- package/squads/squad-council/knowledge-base/innovation-disruption-frameworks.md +193 -0
- package/squads/squad-council/knowledge-base/market-analysis-frameworks.md +240 -0
- package/squads/squad-council/knowledge-base/organizational-leadership-models.md +212 -0
- package/squads/squad-council/knowledge-base/sales-strategy-models.md +215 -0
- package/squads/squad-courses/knowledge-base/course-launch-strategy.md +251 -0
- package/squads/squad-courses/knowledge-base/domain-advocacia-curriculum.md +385 -0
- package/squads/squad-courses/knowledge-base/domain-contabilidade-curriculum.md +266 -0
- package/squads/squad-courses/knowledge-base/platform-comparison.md +68 -0
- package/squads/squad-courses/knowledge-base/video-production-guide.md +70 -0
- package/squads/squad-cybersecurity/knowledge-base/cloud-security-reference.md +363 -0
- package/squads/squad-cybersecurity/knowledge-base/compliance-frameworks.md +273 -0
- package/squads/squad-cybersecurity/knowledge-base/database-security.md +438 -0
- package/squads/squad-cybersecurity/knowledge-base/incident-response-playbook.md +420 -0
- package/squads/squad-cybersecurity/knowledge-base/network-security-reference.md +477 -0
- package/squads/squad-cybersecurity/knowledge-base/penetration-testing-methodology.md +350 -0
- package/squads/squad-cybersecurity/knowledge-base/vulnerability-management.md +349 -0
- package/squads/squad-design/knowledge-base/brazilian-design-context.md +223 -0
- package/squads/squad-design/knowledge-base/component-api-patterns.md +208 -4
- package/squads/squad-design/knowledge-base/design-system-master-reference.md +1302 -0
- package/squads/squad-design/knowledge-base/design-systems-frameworks.md +91 -1
- package/squads/squad-design/knowledge-base/responsive-modern-css.md +96 -4
- package/squads/squad-design/knowledge-base/wcag-aria-reference.md +117 -5
- package/squads/squad-design/knowledge-base/web-performance-reference.md +127 -4
- package/squads/squad-finance/knowledge-base/brazilian-taxation.md +263 -0
- package/squads/squad-finance/knowledge-base/contabilidade-master-reference.md +998 -0
- package/squads/squad-finance/knowledge-base/finance-master-reference.md +946 -0
- package/squads/squad-finance/knowledge-base/financial-reporting-analysis.md +316 -0
- package/squads/squad-finance/knowledge-base/fintech-brazilian-context.md +242 -0
- package/squads/squad-finance/knowledge-base/fpa-planning-frameworks.md +286 -0
- package/squads/squad-finance/knowledge-base/ma-and-transactions.md +285 -0
- package/squads/squad-finance/knowledge-base/risk-management.md +233 -0
- package/squads/squad-finance/knowledge-base/startups-venture-capital.md +337 -0
- package/squads/squad-growth/knowledge-base/ai-growth-playbook.md +216 -0
- package/squads/squad-growth/knowledge-base/attribution-models.md +78 -0
- package/squads/squad-growth/knowledge-base/brazilian-growth-context.md +208 -0
- package/squads/squad-growth/knowledge-base/community-led-growth.md +175 -0
- package/squads/squad-growth/knowledge-base/content-marketing-flywheel.md +190 -0
- package/squads/squad-growth/knowledge-base/email-lifecycle-framework.md +192 -0
- package/squads/squad-growth/knowledge-base/growth-frameworks-catalog.md +82 -0
- package/squads/squad-growth/knowledge-base/growth-master-reference.md +1168 -0
- package/squads/squad-growth/knowledge-base/routing-catalog.md +53 -11
- package/squads/squad-paidmedia/knowledge-base/audiences-segmentation-deep.md +285 -0
- package/squads/squad-paidmedia/knowledge-base/creative-strategy-deep.md +294 -0
- package/squads/squad-paidmedia/knowledge-base/google-ads-account-architecture.md +87 -0
- package/squads/squad-paidmedia/knowledge-base/meta-ads-campaign-architecture.md +76 -0
- package/squads/squad-paidmedia/knowledge-base/paid-media-metrics-reference.md +117 -0
- package/squads/squad-paidmedia/knowledge-base/paid-traffic-master-reference.md +1308 -0
- package/squads/squad-paidmedia/knowledge-base/routing-catalog.md +95 -18
- package/squads/squad-paidmedia/knowledge-base/traffic-masters-frameworks.md +71 -0
- package/squads/squad-product/knowledge-base/brazilian-product-context.md +284 -0
- package/squads/squad-product/knowledge-base/discovery-methodology-playbook.md +141 -0
- package/squads/squad-product/knowledge-base/pm-frameworks-reference.md +125 -9
- package/squads/squad-product/knowledge-base/product-analytics-formulas.md +72 -0
- package/squads/squad-product/knowledge-base/product-led-growth-reference.md +155 -13
- package/squads/squad-product/knowledge-base/product-market-fit-framework.md +222 -0
- package/squads/squad-product/knowledge-base/routing-catalog.md +32 -0
- package/squads/squad-research/knowledge-base/agentic-second-brain-reference.md +591 -0
- package/squads/squad-research/knowledge-base/ai-augmented-research.md +212 -0
- package/squads/squad-research/knowledge-base/brazilian-market-research-sources.md +197 -0
- package/squads/squad-research/knowledge-base/community-platforms-reference.md +786 -0
- package/squads/squad-research/knowledge-base/community-research-methods.md +194 -0
- package/squads/squad-research/knowledge-base/mixed-methods-research-design.md +168 -0
- package/squads/squad-research/knowledge-base/network-effects-analysis.md +192 -0
- package/squads/squad-research/knowledge-base/qualitative-research-deep-methods.md +202 -0
- package/squads/squad-research/knowledge-base/quantitative-research-methods.md +208 -0
- package/squads/squad-research/knowledge-base/research-frameworks-encyclopedia.md +40 -0
- package/squads/squad-research/knowledge-base/research-synthesis-frameworks.md +223 -0
- package/squads/squad-storytelling/knowledge-base/brand-mythology-framework.md +236 -0
- package/squads/squad-storytelling/knowledge-base/brazilian-storytelling-context.md +237 -0
- package/squads/squad-storytelling/knowledge-base/data-storytelling.md +232 -0
- package/squads/squad-storytelling/knowledge-base/improv-storytelling.md +226 -0
- package/squads/squad-storytelling/knowledge-base/persuasion-narrative-techniques.md +269 -0
- package/squads/squad-storytelling/knowledge-base/social-movement-narratives.md +191 -0
- package/squads/squad-storytelling/knowledge-base/video-storytelling.md +252 -0
- package/squads/claude-code-mastery/data/swarm-orchestration-patterns.yaml +0 -378
- package/squads/squad-animations/knowledge-base/framer-motion-complete-reference.md +0 -710
- package/squads/squad-animations/knowledge-base/web-animations-api-view-transitions.md +0 -478
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# Context Window Optimization
|
|
2
|
+
|
|
3
|
+
> Token diet, compaction strategies, and 1M context management. Based on Claude Code internals + MS-009 Agentic Second Brain research.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## The Context Window as a Resource
|
|
8
|
+
|
|
9
|
+
**Karpathy (2025):** "Think of the LLM as a CPU, and its context window as RAM. Your job as an engineer is analogous to an OS: load exactly the right code and data for the task."
|
|
10
|
+
|
|
11
|
+
**Willison (2025):** "Context engineering is what we do instead of fine-tuning."
|
|
12
|
+
|
|
13
|
+
The context window is the scarcest resource in agentic systems. Every token has real cost. Every irrelevant token reduces quality.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Context Budget Breakdown
|
|
18
|
+
|
|
19
|
+
### Claude Code Session Anatomy
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
TOTAL WINDOW: ~200,000 tokens (Sonnet/Opus)
|
|
23
|
+
├── System Prompt (static): ~2,000 tokens (1%) [CACHED]
|
|
24
|
+
├── CLAUDE.md hierarchy: 1-10,000 tokens (0.5-5%) [CACHED]
|
|
25
|
+
├── Rules files (loaded): 500-5,000 tokens (0.25-2.5%) [CACHED]
|
|
26
|
+
├── Tool definitions: ~968 tokens (0.5%) [DEFERRED]
|
|
27
|
+
│ (after deferred loading; was 14-16K without)
|
|
28
|
+
├── Conversation history: grows per turn [COMPACTED]
|
|
29
|
+
├── Tool outputs: variable [BUDGETED]
|
|
30
|
+
└── Response budget: ~50-100K tokens [RESERVED]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Token Sources by Control Level
|
|
34
|
+
|
|
35
|
+
| Source | Typical Size | Your Control |
|
|
36
|
+
|--------|-------------|--------------|
|
|
37
|
+
| System prompt | ~2-4K | None (fixed by CC) |
|
|
38
|
+
| CLAUDE.md (all levels) | 1-10K | Full |
|
|
39
|
+
| .claude/rules/ (loaded) | 0.5-5K | Full (conditional loading) |
|
|
40
|
+
| Conversation history | Grows per turn | Partial |
|
|
41
|
+
| Tool outputs | Variable | Partial (hooks) |
|
|
42
|
+
| MCP tool schemas | ~100-500 per server | Partial |
|
|
43
|
+
| Tool definitions | ~968 (deferred) or 14-16K (upfront) | Full |
|
|
44
|
+
|
|
45
|
+
**CLAUDE.md and rules = highest leverage targets** (full control, loaded every turn).
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Claude Code Compaction System
|
|
50
|
+
|
|
51
|
+
### 5-Layer Pipeline
|
|
52
|
+
|
|
53
|
+
| Layer | Strategy | API Cost | When |
|
|
54
|
+
|-------|----------|----------|------|
|
|
55
|
+
| **Tool Result Budget** | Caps individual result sizes | 0 | Continuous |
|
|
56
|
+
| **Snip Compact** | Removes unnecessary intermediate messages | 0 | As needed |
|
|
57
|
+
| **MicroCompact** | Merges consecutive tool-result/user pairs | 0 | Per turn |
|
|
58
|
+
| **Context Collapse** | Read-time projection over history | 0 | Near limit |
|
|
59
|
+
| **AutoCompact** | Complete summarization via forked agent | 1 | ~95% capacity |
|
|
60
|
+
|
|
61
|
+
**AutoCompact trigger:** ~95% context window capacity (25% remaining).
|
|
62
|
+
|
|
63
|
+
### What Survives Compaction
|
|
64
|
+
|
|
65
|
+
**Preserved:**
|
|
66
|
+
- CLAUDE.md (re-read from disk after each compaction)
|
|
67
|
+
- User requests and key code snippets
|
|
68
|
+
- "Compact Instructions" from CLAUDE.md
|
|
69
|
+
- Active plans and pending work
|
|
70
|
+
|
|
71
|
+
**Lost:**
|
|
72
|
+
- Instructions given in conversation (NOT in CLAUDE.md) — **put important instructions in CLAUDE.md, not chat**
|
|
73
|
+
- Error messages, line numbers, variable values, stack traces
|
|
74
|
+
- Reasoning behind decisions
|
|
75
|
+
- Specific debugging details
|
|
76
|
+
|
|
77
|
+
### Compact Instructions Section
|
|
78
|
+
|
|
79
|
+
Add to CLAUDE.md to control what AutoCompact preserves:
|
|
80
|
+
|
|
81
|
+
```markdown
|
|
82
|
+
## Compact instructions
|
|
83
|
+
- Preserve code paths and unresolved security questions
|
|
84
|
+
- Preserve diff summaries and failed test output
|
|
85
|
+
- Preserve current story ID and acceptance criteria
|
|
86
|
+
- Preserve any pending tasks and their status
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Token Reduction Techniques
|
|
92
|
+
|
|
93
|
+
### 1. Deferred Tool Loading (93% reduction on tool definitions)
|
|
94
|
+
|
|
95
|
+
The biggest single optimization since v2.1.69:
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
Before: ~14,000-16,000 tokens for all tool definitions
|
|
99
|
+
After: ~968 tokens (ToolSearch + non-deferred tools)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Tools are discovered on-demand via `ToolSearchTool`. Only activated tools add to context.
|
|
103
|
+
|
|
104
|
+
### 2. Static/Dynamic Prompt Separation
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
[Static — before boundary] ← Cached, shared across all users
|
|
108
|
+
Identity, guidelines, tasks,
|
|
109
|
+
actions, tools, tone, output
|
|
110
|
+
─────────────────────────────
|
|
111
|
+
[SYSTEM_PROMPT_DYNAMIC_BOUNDARY]
|
|
112
|
+
─────────────────────────────
|
|
113
|
+
[Dynamic — after boundary] ← Not cached, session-specific
|
|
114
|
+
Project context, CLAUDE.md,
|
|
115
|
+
memory, env info, MCP instructions
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Cache hit rate** = biggest cost lever. A cache miss on static content = **5x cost for that turn**.
|
|
119
|
+
|
|
120
|
+
### 3. Rules File Conditional Loading
|
|
121
|
+
|
|
122
|
+
Without `paths:` frontmatter: rule loads globally (every interaction).
|
|
123
|
+
With `paths:` frontmatter: rule loads only when editing matching files.
|
|
124
|
+
|
|
125
|
+
```yaml
|
|
126
|
+
---
|
|
127
|
+
paths:
|
|
128
|
+
- "src/api/**/*.ts"
|
|
129
|
+
---
|
|
130
|
+
# API Route Rules
|
|
131
|
+
Only loaded when editing API files.
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Strategy:** Convert global rules to scoped rules wherever possible.
|
|
135
|
+
|
|
136
|
+
### 4. CLAUDE.md Token Diet
|
|
137
|
+
|
|
138
|
+
**High-value content (include):**
|
|
139
|
+
- Project identity (1 sentence)
|
|
140
|
+
- Technology stack (table format)
|
|
141
|
+
- Non-obvious directory structure
|
|
142
|
+
- Conventions that differ from standard practices
|
|
143
|
+
- Anti-patterns (prevents costly mistakes)
|
|
144
|
+
- Common commands (build, test, lint)
|
|
145
|
+
- Known gotchas
|
|
146
|
+
|
|
147
|
+
**Low-value content (exclude):**
|
|
148
|
+
- Generic programming advice (Claude already knows)
|
|
149
|
+
- Standard framework conventions (React, Express, etc.)
|
|
150
|
+
- Obvious file purposes
|
|
151
|
+
- Long code examples (use one-liners or references)
|
|
152
|
+
- Historical context (why decisions were made)
|
|
153
|
+
- Duplicate information
|
|
154
|
+
|
|
155
|
+
### 5. Document Sharding (BMAD Pattern)
|
|
156
|
+
|
|
157
|
+
Large documents split into focused shards:
|
|
158
|
+
|
|
159
|
+
| Format | Token Size |
|
|
160
|
+
|--------|-----------|
|
|
161
|
+
| Single large PRD | ~5,000 tokens |
|
|
162
|
+
| Sharded (per section) | ~300 tokens each |
|
|
163
|
+
| **Savings** | **74-90%** |
|
|
164
|
+
|
|
165
|
+
Only load the shard relevant to current task.
|
|
166
|
+
|
|
167
|
+
### 6. Image Optimization
|
|
168
|
+
|
|
169
|
+
| Image Size | Token Cost |
|
|
170
|
+
|-----------|-----------|
|
|
171
|
+
| 200×200 pixels | 54 tokens |
|
|
172
|
+
| 1000×1000 pixels | 1,334 tokens |
|
|
173
|
+
| **Savings with cropping** | **25x** |
|
|
174
|
+
|
|
175
|
+
Always crop/resize images to minimum necessary for the task.
|
|
176
|
+
|
|
177
|
+
### 7. File Format Efficiency
|
|
178
|
+
|
|
179
|
+
| Format | Token Efficiency vs JSON | Notes |
|
|
180
|
+
|--------|------------------------|-------|
|
|
181
|
+
| Markdown | -16% tokens | Best for instructions, docs |
|
|
182
|
+
| YAML | Better for nested | 62% accuracy in nested structures |
|
|
183
|
+
| JSON | Baseline | Standard for schemas, APIs |
|
|
184
|
+
|
|
185
|
+
**Recommendation:**
|
|
186
|
+
- CLAUDE.md: Markdown
|
|
187
|
+
- Config: YAML frontmatter + Markdown body
|
|
188
|
+
- Skills/Agents: YAML frontmatter in `.md` files
|
|
189
|
+
- Settings: JSON
|
|
190
|
+
- Files: < 300 lines each
|
|
191
|
+
|
|
192
|
+
### 8. Model Routing (Tiered)
|
|
193
|
+
|
|
194
|
+
Not all decisions require frontier-class inference:
|
|
195
|
+
|
|
196
|
+
| Decision Type | Model | Cost Multiplier |
|
|
197
|
+
|-------------|-------|----------------|
|
|
198
|
+
| Planning complex tasks | Opus | 1.0x (baseline) |
|
|
199
|
+
| Code implementation | Sonnet | 0.33x |
|
|
200
|
+
| Permission checks | Haiku | 0.067x |
|
|
201
|
+
| Frustration detection | Regex | ~0x |
|
|
202
|
+
| Context compression (micro) | None (no API call) | 0x |
|
|
203
|
+
|
|
204
|
+
**Savings:** 40-70% total cost reduction with proper routing.
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## 14 Cache-Break Vectors
|
|
209
|
+
|
|
210
|
+
Claude Code monitors 14 vectors that invalidate prompt cache:
|
|
211
|
+
|
|
212
|
+
1. Adding an MCP tool
|
|
213
|
+
2. Putting timestamp in system prompt (use in message instead)
|
|
214
|
+
3. Switching model mid-session
|
|
215
|
+
4. Changing images in prompt
|
|
216
|
+
5. Modifying tool settings
|
|
217
|
+
6. Adding/removing CLAUDE.md files
|
|
218
|
+
7. Changing rule files
|
|
219
|
+
8. Updating agent definitions
|
|
220
|
+
9. Modifying hook configurations
|
|
221
|
+
10. Changing environment variables in settings
|
|
222
|
+
11. Adding new MCP server instructions
|
|
223
|
+
12. Updating agent memory
|
|
224
|
+
13. Modifying project structure detected by hooks
|
|
225
|
+
14. Permission mode changes
|
|
226
|
+
|
|
227
|
+
**Sticky latches:** `afkModeHeaderLatched` and `fastModeHeaderLatched` prevent cache busts when toggling settings mid-session. Once latched, remains latched for session duration.
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Context Engineering for Multi-Agent
|
|
232
|
+
|
|
233
|
+
### Cache Sharing Between Subagents
|
|
234
|
+
|
|
235
|
+
Fork children use **byte-identical system prompt prefixes** to share KV cache:
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
Parent system prompt (cached):
|
|
239
|
+
[System guidelines]
|
|
240
|
+
[Project context]
|
|
241
|
+
[CLAUDE.md]
|
|
242
|
+
[Tool definitions]
|
|
243
|
+
────────────────────
|
|
244
|
+
[Agent-specific task] ← unique per agent
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Cost:** "Spawning five forked agents costs barely more than 1."
|
|
248
|
+
|
|
249
|
+
**Requirement:** Agent prompts must be designed to maximize shared prefix length.
|
|
250
|
+
|
|
251
|
+
### Agent Memory Context Budget
|
|
252
|
+
|
|
253
|
+
For SINAPSE agents, recommended allocation:
|
|
254
|
+
|
|
255
|
+
```
|
|
256
|
+
200K token session:
|
|
257
|
+
Agent persona (CLAUDE.md + rules): ~5K (2.5%)
|
|
258
|
+
HOT memory (current task): ~20K (10%)
|
|
259
|
+
WARM memory (retrieved context): ~30K (15%)
|
|
260
|
+
Conversation history (compacted): ~20K (10%)
|
|
261
|
+
Tool outputs (last N results): ~50K (25%)
|
|
262
|
+
Response budget: ~75K (37.5%)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## The `lost-in-the-middle` Effect
|
|
268
|
+
|
|
269
|
+
Research finding: LLMs show significantly degraded performance when critical information appears in the **middle** of a long context. Performance peaks when information is at the beginning or end.
|
|
270
|
+
|
|
271
|
+
**Mitigation strategies:**
|
|
272
|
+
1. Put most important instructions in system prompt (beginning)
|
|
273
|
+
2. Put current task at end of context (just before response)
|
|
274
|
+
3. Use compaction to remove middle noise
|
|
275
|
+
4. Structure long contexts with clear section headers
|
|
276
|
+
5. Use explicit attention anchors ("IMPORTANT:", "CRITICAL:")
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## Practical Optimization Checklist
|
|
281
|
+
|
|
282
|
+
### Before Starting a Session
|
|
283
|
+
|
|
284
|
+
- [ ] CLAUDE.md is under 200 lines
|
|
285
|
+
- [ ] No global rules that should be scoped rules
|
|
286
|
+
- [ ] Images cropped to necessary size
|
|
287
|
+
- [ ] `ENABLE_TOOL_SEARCH=auto` configured
|
|
288
|
+
- [ ] Compact instructions section in CLAUDE.md
|
|
289
|
+
|
|
290
|
+
### Before Each Agent Turn
|
|
291
|
+
|
|
292
|
+
- [ ] Is conversation history manageable? (< 50% of budget)
|
|
293
|
+
- [ ] Have tool outputs been budgeted? (large outputs truncated)
|
|
294
|
+
- [ ] Is the current task clearly specified in last message?
|
|
295
|
+
- [ ] Any irrelevant context that should be cleared?
|
|
296
|
+
|
|
297
|
+
### Red Flags (Memory Full Indicators)
|
|
298
|
+
|
|
299
|
+
- Claude asking for information it should have from CLAUDE.md
|
|
300
|
+
- Claude losing track of requirements mid-task
|
|
301
|
+
- Responses getting shorter and less detailed
|
|
302
|
+
- Claude proposing solutions already tried
|
|
303
|
+
- `/compact` triggered automatically
|
|
304
|
+
|
|
305
|
+
**Action on red flags:** Use `/compact` or `/clear` immediately. Or add key context to CLAUDE.md so it survives compaction.
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## SINAPSE Context Architecture
|
|
310
|
+
|
|
311
|
+
### System Prompt Allocation
|
|
312
|
+
|
|
313
|
+
```
|
|
314
|
+
~/.claude/CLAUDE.md (global): ~2K tokens
|
|
315
|
+
Project CLAUDE.md: ~3K tokens
|
|
316
|
+
.claude/rules/ (all): ~5K tokens
|
|
317
|
+
──────────────────────────────────
|
|
318
|
+
Total baseline: ~10K tokens
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### Agent Memory Allocation
|
|
322
|
+
|
|
323
|
+
Each SINAPSE agent file: target < 300 lines / ~3K tokens.
|
|
324
|
+
|
|
325
|
+
**Why:** Agents are loaded into context when activated. Large agent definitions increase baseline cost for every session turn.
|
|
326
|
+
|
|
327
|
+
### KB Files in Context
|
|
328
|
+
|
|
329
|
+
Knowledge base files are NOT automatically loaded. They are:
|
|
330
|
+
- Referenced in CLAUDE.md or rules
|
|
331
|
+
- Loaded on-demand when agent needs specific knowledge
|
|
332
|
+
- Ideal for: reference material, patterns, examples
|
|
333
|
+
|
|
334
|
+
**Recommendation:** Keep KB files as standalone reference documents. Link from CLAUDE.md with `see squads/squad-claude/knowledge-base/X.md` comments, not full @include.
|