Loading...
Loading...
Cognitive Scaffolding structures an agent's context window using principles from cognitive science — primacy effects, recency bias, chunking, and attention allocation.
npx skill4agent add itallstartedwithaidea/agent-skills cognitive-scaffoldinggraph LR
subgraph "Context Window"
A[Anchor Zone<br/>5% - Highest Attention<br/>Identity, Rules] --> B[Structured Middle<br/>60% - Chunked Data<br/>Delimited Sections]
B --> C[Background Zone<br/>15% - Reference<br/>Fallback Material]
C --> D[Foreground Zone<br/>20% - Current Task<br/>Recent Messages]
end
E[Primacy Effect] -.-> A
F[Chunking Theory] -.-> B
G[Low Attention Region] -.-> C
H[Recency Effect] -.-> Dinterface CognitiveZone {
name: string;
position: "anchor" | "middle" | "background" | "foreground";
budgetPercent: number;
content: string;
delimiter: string;
}
class CognitiveScaffold {
private totalBudget: number;
private zones: Map<string, CognitiveZone> = new Map();
constructor(totalTokenBudget: number) {
this.totalBudget = totalTokenBudget;
}
setAnchor(content: string): void {
this.zones.set("anchor", {
name: "Identity & Rules",
position: "anchor",
budgetPercent: 5,
content,
delimiter: "",
});
}
addMiddleChunk(name: string, content: string): void {
const key = `middle_${this.zones.size}`;
this.zones.set(key, {
name,
position: "middle",
budgetPercent: 0,
content,
delimiter: `\n<section name="${name}">\n`,
});
}
setForeground(content: string): void {
this.zones.set("foreground", {
name: "Current Task",
position: "foreground",
budgetPercent: 20,
content,
delimiter: "\n<current_task>\n",
});
}
assemble(): string {
const sections: string[] = [];
const anchor = this.zones.get("anchor");
if (anchor) sections.push(anchor.content);
const middle = [...this.zones.entries()]
.filter(([_, z]) => z.position === "middle")
.map(([_, z]) => `${z.delimiter}${z.content}\n</section>`);
sections.push(...middle);
const bg = [...this.zones.entries()]
.filter(([_, z]) => z.position === "background");
for (const [_, zone] of bg) {
sections.push(`<reference name="${zone.name}">\n${zone.content}\n</reference>`);
}
const fg = this.zones.get("foreground");
if (fg) sections.push(`${fg.delimiter}${fg.content}\n</current_task>`);
return sections.join("\n\n");
}
}class AttentionOptimizer:
"""Place content based on importance and model attention patterns."""
ATTENTION_CURVE = {
"anchor": 0.95,
"early_middle": 0.60,
"deep_middle": 0.40,
"late_middle": 0.55,
"foreground": 0.90,
}
def optimize_placement(self, items: list[dict]) -> list[dict]:
"""Sort items into optimal positions based on importance score."""
sorted_items = sorted(items, key=lambda x: x["importance"], reverse=True)
zones = {zone: [] for zone in self.ATTENTION_CURVE}
zone_order = sorted(self.ATTENTION_CURVE.keys(), key=lambda z: self.ATTENTION_CURVE[z], reverse=True)
for item in sorted_items:
best_zone = min(zone_order, key=lambda z: abs(self.ATTENTION_CURVE[z] - item["importance"]))
zones[best_zone].append(item)
placement = []
for zone in ["anchor", "early_middle", "deep_middle", "late_middle", "foreground"]:
for item in zones[zone]:
placement.append({**item, "zone": zone, "expected_attention": self.ATTENTION_CURVE[zone]})
return placementdef chunk_for_middle_zone(data: list[dict], chunk_size: int = 5) -> list[str]:
"""Break data into cognitively manageable chunks with clear boundaries."""
chunks = []
for i in range(0, len(data), chunk_size):
batch = data[i:i + chunk_size]
header = f"--- Chunk {i // chunk_size + 1} of {(len(data) + chunk_size - 1) // chunk_size} ---"
body = "\n".join(format_item(item) for item in batch)
summary = f"Summary: {len(batch)} items, key values: {extract_key_values(batch)}"
chunks.append(f"{header}\n{body}\n{summary}")
return chunks
def build_scaffolded_prompt(task, data, rules):
scaffold = CognitiveScaffold(total_token_budget=150000)
scaffold.set_anchor(f"""You are Buddy™, a Google Ads analysis agent.
RULES (always enforced):
{chr(10).join(f'- {r}' for r in rules)}""")
for i, chunk in enumerate(chunk_for_middle_zone(data)):
scaffold.add_middle_chunk(f"data_chunk_{i}", chunk)
scaffold.set_foreground(f"""CURRENT TASK:
{task}
Analyze the data in the sections above and provide your recommendation.""")
return scaffold.assemble()| Feature | Claude Code | Cursor | Codex | Gemini CLI |
|---|---|---|---|---|
| Context structuring | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
| XML delimiters | ✅ Preferred | ✅ Supported | ✅ Supported | ✅ Supported |
| Token budget control | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
| Attention optimization | ✅ Claude-tuned | ✅ Model-dependent | ✅ Model-dependent | ✅ Gemini-tuned |
| Zone-based assembly | ✅ Full | ✅ Full | ✅ Full | ✅ Full |