ASK KNOX
beta
LESSON 67

Shared State and Agent Memory: The Coordination Layer

Agents are stateless by default. They finish a run and remember nothing. Coordination across agents requires an explicit shared state layer. File-based state, lock mechanisms, event logs, and memory systems — the infrastructure that makes multi-agent systems coherent.

10 min read·Multi-Agent Orchestration

Here is the fundamental problem with multi-agent systems: agents are stateless processes.

An agent spawns, executes its task, terminates. It has no memory of previous runs. It has no awareness of what other agents are doing. It cannot look over at the agent running next to it and ask "what did you find?" It is isolated by design — that isolation is what makes parallelization safe. But isolation also means coordination requires explicit infrastructure.

The solution is not complex. The state layer is a set of files on disk. Agents read from it. Agents write to it. A lock mechanism prevents corruption. An event log provides an audit trail. A memory system provides semantic recall across longer time horizons. That is the full architecture of most production multi-agent state management.

Shared State Layer — Agents Reading and Writing

The Stateless Agent Problem

When the blog-autopilot researcher finishes its job, it does not remember the result. The next time a researcher runs — even minutes later — it starts fresh. This is correct behavior. Statelessness is what makes agents restartable, reproducible, and safe to parallelize.

But the writer agent needs the researcher's findings. How does the researcher communicate those findings to the writer if the researcher is gone?

It wrote them to a file before it terminated. The writer reads the file. The researcher's memory is externalized — it lives in the filesystem, not in the agent's process memory.

This pattern scales to an entire fleet. Every piece of state that needs to persist past one agent's execution or be accessible to more than one agent must be externalized to the state layer. If it is not in the state layer, it does not exist for any other agent.

The File-Based State Layer

The practical state layer for most production multi-agent systems:

state.json — current run state. What has completed, what is pending, what errors have occurred. Updated by each agent as it starts and finishes.

results/ — agent-specific output files. research.json, script.md, images/. Each agent writes its output here under a predictable name.

events.jsonl — append-only event log. Every agent action is appended as a JSON line with timestamp, agent ID, action type, and outcome. Never modified — only appended.

Lock filesstate.json.lock exists while an agent is writing to state.json. Other agents that need to write check for the lock first.

This architecture requires no message broker, no database, no network dependency. It runs on any machine with a filesystem. A semantic memory layer — vector DB indexing thousands of chunks of accumulated operational knowledge — can be built on top of this, adding semantic recall for longer-horizon context. But the base layer is just files.

The Lock Protocol

Write conflicts are the primary failure mode of file-based state coordination. Two agents simultaneously writing to state.json produce a corrupted file. The lock protocol prevents this.

The protocol, in order:

  1. Before writing, check for state.json.lock
  2. If absent: create state.json.lock, write to state.json, delete state.json.lock
  3. If present: wait with exponential backoff (100ms, 200ms, 400ms...) up to a maximum retry count, then fail with an explicit error

The implementation is four lines of Python. The failure to implement it produces intermittent corruption that is nearly impossible to debug without the audit trail from the event log.

The Event Log as Debugging Infrastructure

The event log (events.jsonl) is the single most valuable debugging tool in a production multi-agent system. It answers the question "what did this fleet actually do?" after the fact.

Every entry should include:

  • timestamp — ISO 8601, millisecond precision
  • agent_id — which agent generated this event
  • run_id — which pipeline run this belongs to
  • event_type — "started", "completed", "failed", "state_written", "state_read"
  • payload — relevant context for the event type

When the writer agent produces a blank article at 3 AM and you are trying to figure out why, the event log tells you: the researcher ran at 2:58 AM, wrote research.json at 2:59 AM (status: partial — API rate limited), and the writer read it at 3:00 AM before the partial write was flagged. The research was incomplete. The writer wrote what it had.

That diagnosis takes 30 seconds with an event log. It takes hours without one.

Memory Systems for Cross-Agent Knowledge

File-based state handles run-level coordination. It does not handle knowledge that should persist across weeks or months of runs.

A semantic memory layer — a vector store exposed to agents via MCP — solves this. When the blog-autopilot researcher discovers that a particular content angle performs consistently well, that insight gets stored in the memory layer. The next researcher, running two days later, queries the memory layer before starting its web search: "what content angles have worked well recently?" It gets a semantic answer from accumulated cross-run knowledge.

This is the compound memory effect at the fleet level. Individual runs produce state. The state layer captures it. The memory system indexes it. Future agents query it. The fleet gets incrementally smarter without any single agent knowing more than its context window allows.

The parallel: a fleet with persistent memory knows both the domain (accumulated research) and itself (what has worked before). That knowledge compounds across runs.

Practical State Layer Design

When designing a state layer for a new fleet, three questions:

1. What state needs to persist between agent runs? Run-level results (ephemeral, can recalculate). Cross-run knowledge (persistent, needs memory system). Both? Design accordingly.

2. What is the write contention? If only one agent writes to a given file, you may not need locking. If multiple agents write to the same file (common with state.json), you need the full lock protocol.

3. What does debugging require? At minimum, the event log. If timing matters, millisecond timestamps. If you need replay capability, the event log should capture enough state to reconstruct what happened without access to the original agents.

Lesson 67 Drill

For a multi-agent workflow you are planning:

  1. List every piece of state that must survive one agent's execution to be read by another agent.
  2. For each piece: what is the file name? What is the schema?
  3. Which state files will have more than one writer? Those need the lock protocol.
  4. Design the event log schema: what fields does every entry include?
  5. Is there cross-run knowledge that should be indexed in a memory system rather than overwritten each run?

Those answers are your state layer specification. Write it before writing any agent.

Bottom Line

Agents are stateless. Coordination requires external state.

Files are the message bus. Locks prevent corruption. Event logs enable debugging. Memory systems enable learning across runs.

The state layer is not complex to build. But it must be designed intentionally, before agents are implemented, not retrofitted after they are already failing silently on write conflicts at 3 AM.

Build the state layer first. Then build the agents.