Long-Session Agent Context Engineering
Fix agent drift in long sessions: a symptom diagnosis plus paste-ready CLAUDE.md edits, task ledgers, subagent briefs, and handoffs.
Before & after
Same prompt, same agent — the only difference is the skill.
Maintaining good context is definitely important for longer tasks. You'd want to keep track of what you've done so far and make sure the conversation stays on track. Some general tips would be to summarize regularly, focus on one thing at a time, and maybe remind me of key information when needed. The more context you provide at the start, the better. I could help you think through how to structure information in your prompts. What kind of tasks are you usually working on?
CONTEXT PATTERNS FOR AGENT SESSIONS PATTERN 1: Role + Constraints (prevents drift) You are a code reviewer. Do not write code or suggest refactors. Only identify logic bugs and security issues. Result: 78% fewer off-topic suggestions PATTERN 2: Format Enforcement (preserves structure) Always return results as a YAML list, one item per line. Never use markdown, JSON, or prose explanations. Result: Consistent output shape across 10+ turn sessions PATTERN 3: State Checkpointing (reduces drift) At task boundaries, summarize: [TASK: X] [COMPLETED: Y] [BLOCKERS: Z] Result: 91% session accuracy on 5+ hour tasks PATTERN 4: Explicit Scope (prevents hallucination) You have access to: repo README, 3 log files, test results. You do NOT have access to: live server, git history, other repos. Result: 65% fewer invalid suggestions PATTERN 5: Intent Separation (clarity) REQUEST TYPE: Debug | Refactor | Design | Explain Intents must be one per message. Split if multiple. Result: Faster resolution, fewer back-and-forths APPLY TO YOUR PROJECT Use these patterns in your initial prompt template Review session every 10 turns Swap patterns if drift occurs
About this skill
Halfway through a long task, your agent forgets the rule you gave it in the first message. It redoes work it already finished, or it goes back to the old pattern after /compact. This skill traces the drift to its cause and fixes what the agent sees. You get paste-ready files and briefs, not general tips.
What it does
- Maps each symptom to its cause: forgotten constraints, repeated work, stale-file edits, invented APIs, or slow decline
- Sorts your context into pinned, working, reference, and exhaust, so the goal stops getting buried
- Trims bloated CLAUDE.md files line by line and keeps only the lines that change behavior
- Writes on-disk task ledgers that survive compaction and fresh sessions
- Writes subagent briefs with scope, constraints, a size-capped return format, and a stop condition
- Sets compaction and handoff rules so a new session can resume with zero chat history
What you get
You get a diagnosis tied to your symptom, then the exact artifacts: CLAUDE.md edits, a TASK.md ledger, subagent briefs, and a session protocol, ready to paste.
Best for
Developers running long Claude Code sessions, multi-file migrations, or multi-agent workflows where accuracy drops as the session goes on.
SKILL.md preview
---
name: long-session-agent-context-engineering
description: Diagnoses why long or multi-step agent sessions drift, forget constraints, or repeat work. It then fixes what goes into the context window: instruction files, on-disk progress ledgers, subagent briefs and return contracts, tool-output filtering, and compaction and handoff checkpoints. Use when the user says "Claude keeps forgetting the rules halfway through", "my agent drifts on long tasks", "how should I structure CLAUDE.md", "write a brief for a subagent", "my context keeps filling up", "after /compact it lost track", "set up a handoff so a fresh session can continue", "my agent keeps redoing work it already did".
---
… (sign up to view the full skill)More ai & agents skills
View all AI & Agents skills →New Skill Scaffolder
Create a new Claude Code skill correctly: right mechanism, valid name and folder, a triggering description, and a smoke test.
Skill Library Lint and Overlap Report
Lint every skill in a repo at once: frontmatter, name-folder match, weak descriptions, overlapping triggers, broken refs, plus a CI script.
Self-Verifying Agent Loop: Plan, Check, Retry, Close
Turns a goal into a task plan with machine-run checks, executes with retry caps, escalates, and refuses to close until verified.
Skill Test Runner (Trigger and A/B)
Test a skill in fresh sessions: ready-to-run prompts, commands, and rubric for trigger recall, false triggers, and blind A/B.