DEV Community

Patrick
Patrick

Posted on • Originally published at askpatrick.co

The Self-Contract Pattern: Why Scalable AI Agents Have Three Required Files

Most AI agents fail the same way: they drift.

Not dramatically. Gradually. The agent that was crisp and reliable on day one starts hallucinating its own scope on day thirty. It tries things it shouldn't. It forgets things it should know. It loses the thread.

The root cause is almost always the same: the agent has no written contract with itself.

Here's what I mean — and the three-file pattern that fixes it.

The Problem with "Training" as the Only Contract

Lots of teams rely on the base model + system prompt to define what their agent is. That works fine at small scale. But it has a fatal flaw: context dilutes over time.

Every session loads a bit more. Every memory appended to the system prompt adds noise. By the time you're running an agent in production with months of accumulated context, the original intent is buried.

The agent technically "knows" what it is — but the signal is weak and the noise is high.

The Self-Contract: Three Files, Total Clarity

A self-contracting agent reloads its identity at the start of every session. Not from the model weights. From explicit files it reads on startup.

File 1: SOUL.md — Identity and Constraints

This is who the agent is and what it will never do. Not a list of capabilities — a list of commitments.

Example (abbreviated):

You are Kai, operations agent for Ask Patrick.
You manage cron schedules, file I/O, and system health.
You NEVER send messages to external users without approval.
You NEVER delete files — move to trash only.
You escalate to Patrick if: cost > $50/day, error rate > 10%, any action outside your defined scope.
Enter fullscreen mode Exit fullscreen mode

Short. Specific. Reloaded every session. The agent can't "forget" its constraints if it reads them fresh every time.

File 2: MEMORY.md — What It Learned

This is curated long-term memory. Not a log — a distilled summary of lessons, context, and decisions that matter.

The key discipline: MEMORY.md is curated, not append-only. Bad long-term memory grows forever and becomes noise. Good long-term memory gets reviewed, pruned, and updated. The agent should periodically review its own memory and remove what's no longer relevant.

This is how humans actually work. We don't remember every conversation — we distill the important parts into a mental model that updates over time.

File 3: current-task.json — What It's Doing Right Now

This is state. A simple JSON file the agent reads at startup and writes at the end of each session.

{
  "task": "content-loop",
  "phase": "post-tweet",
  "started": "2026-03-08T06:00:00Z",
  "last_action": "devto-publish",
  "notes": "X API rate-limited until ~9 AM"
}
Enter fullscreen mode Exit fullscreen mode

Why does this matter? Because agents in loops can lose track of where they are. A current-task file is a single source of truth that survives session restarts, rate limits, and interruptions.

Without it, the agent has to infer state from context. Inference is expensive and error-prone. A 100-byte JSON file is free.

The Reload Pattern

The critical piece: these files get reloaded at the start of every session, not just once at setup.

Before every session:
1. Read SOUL.md → reinforce identity and constraints
2. Read MEMORY.md → load long-term context
3. Read current-task.json → load current state
Enter fullscreen mode Exit fullscreen mode

This takes milliseconds and adds a few hundred tokens to your context window. The payoff: the agent's behavior stays consistent across sessions, restarts, and model updates.

Why This Works

The three-file pattern works because it treats consistency as a system property, not a model property.

You can't rely on the model to stay consistent. Models drift. Fine-tunes degrade. Context windows fill. But a file system is static until you change it. When your agent's identity lives in a file it reads every session, you have a stable source of truth that you can audit, version control, and update deliberately.

The agent that scales is the one with a written contract it enforces on itself.

Where to Go From Here

If you want the actual SOUL.md templates, MEMORY.md patterns, and current-task.json schemas we use in production — they're in the Ask Patrick Library.

New configs drop nightly. Library + Daily Briefing access is $9/month.

askpatrick.co/library


Ask Patrick is a resource for operators running AI agent teams. Real configs, real metrics, real operations.

Top comments (1)

Collapse
 
nyrok profile image
Hamza KONTE

"Context dilutes over time" is the precise failure mode — and a written contract is the right antidote because it's the one thing that doesn't dilute. Every new session re-reads the contract; it's not competing with accumulated memory for attention weight.

The three-file pattern (SOUL.md, MEMORY.md, current-task.json) maps cleanly to three layers of prompt structure: identity/constraints (SOUL), context (MEMORY), and objective/goal (current-task). What's interesting is that you've essentially turned those semantic layers into persistent documents rather than inline prompt blocks — which solves the context dilution problem because they're loaded freshly each session rather than accumulating.

The prompt engineering corollary: the same block separation that makes these files durable is what flompt (flompt.dev) applies at the single-session level — breaking prompts into typed semantic blocks (role, context, objective, constraints) that are edited independently rather than accumulating into an undifferentiated blob.

Open-source: github.com/Nyrok/flompt