How to Structure AI Agent Prompts for Long-Running Tasks

Fazm Team··3 min read

How to Structure AI Agent Prompts for Long-Running Tasks

Short AI agent tasks are easy - give a clear instruction, get a result. Long-running tasks that span hours are a different challenge entirely. The agent drifts, forgets constraints, and produces increasingly inconsistent output. Here is how to prevent that.

The Drift Problem

After 30-60 minutes of continuous work, AI agents start losing coherence. Early instructions get compressed or forgotten as the context window fills up. The agent that was carefully following your coding conventions in minute 10 is ignoring them by minute 45.

This is not a model flaw - it is a context window limitation. Information at the beginning of the window gets less attention as new content pushes it further back.

Checkpoint Architecture

Break long tasks into explicit checkpoints:

  1. Define milestones upfront - "This task has 5 phases. Complete each one, then summarize what was done before starting the next."
  2. Force summaries - At each checkpoint, have the agent write a brief summary of completed work and remaining tasks
  3. Re-inject constraints - Repeat critical rules at each checkpoint. Your coding conventions, naming patterns, and architectural boundaries need to be restated, not assumed

The Running Log Pattern

Maintain a running log file that the agent updates throughout the session:

## Task: Migrate database layer to async
## Constraints: No breaking changes to API, maintain backwards compat
## Progress:
- [x] Phase 1: Audit existing sync calls (14 found)
- [ ] Phase 2: Create async wrappers
- [ ] Phase 3: Update callers
- [ ] Phase 4: Remove sync versions
- [ ] Phase 5: Update tests

The agent reads this file at each phase transition, which refreshes its understanding of the overall task and constraints.

Scoped Sessions Over Marathon Sessions

Instead of one 4-hour session, run four 1-hour sessions with explicit handoff:

  • End each session with a summary written to a file
  • Start the next session by reading that summary
  • Each session gets a fresh context window with all constraints at full strength

This approach trades some continuity for dramatically better consistency.

Key Rules

  • Never assume the agent remembers instructions from more than 30 minutes ago
  • Repeat critical constraints at every phase boundary
  • Use files as external memory, not the context window
  • Prefer multiple focused sessions over single marathon sessions

The agents that handle long tasks well are not smarter - they are better structured.

Fazm is an open source macOS AI agent. Open source on GitHub.

More on This Topic

Related Posts