Velamuvi AIAcademy

Lesson 5 of 6 · Concepts

Shared State

Let agents share information safely: blackboards, lost updates, versioned compare-and-set writes, ownership rules, append-only logs and artifacts by reference.

  • 6 min read
  • Advanced
  • Reviewed
In this lesson you will learn
  • Explain when agents should share state instead of passing messages
  • Recognise the lost-update problem when two agents write at once
  • Prevent silent overwrites with versioned compare-and-set writes
  • Use ownership, append-only logs and references to keep shared state simple

Working from the same page

Messages work well for handing over tasks and results. But some information is needed by many agents at once: the remaining budget, the trip's dates, the draft report, the list of sources already checked. Copying it into every message is wasteful and quickly goes out of date.

The lost update

Shared state brings a classic problem. Two agents read the remaining budget, 2,000 euros, at the same time. The flight agent spends 600 and writes 1,400. The hotel agent spends 900 and writes 1,100, based on the 2,000 it read. The flight's 600 has silently vanished from the books.

The fix is to give every entry a version number and only accept a write from an agent that saw the latest version:

Try it yourselfPython
# A shared blackboard: agents read and write named entries. Every entry has a version number,
# and a write only succeeds if the writer saw the latest version (no silent overwrites).
class Blackboard:
    def __init__(self):
        self.data, self.log = {}, []

    def read(self, key):
        return self.data.get(key, (None, 0))                 # (value, version)

    def write(self, key, value, seen_version, agent):
        _, current = self.read(key)
        if seen_version != current:
            self.log.append(f"{agent}: CONFLICT on {key} (saw v{seen_version}, now v{current})")
            return False
        self.data[key] = (value, current + 1)
        self.log.append(f"{agent}: {key} = {value!r} (v{current + 1})")
        return True

board = Blackboard()
board.write("budget_left", 2000, seen_version=0, agent="lead")

# Two agents read the budget at the same time...
flights_value, flights_seen = board.read("budget_left")
hotel_value, hotel_seen = board.read("budget_left")

# ...and both try to spend from it
board.write("budget_left", flights_value - 600, flights_seen, agent="flights")     # succeeds
if not board.write("budget_left", hotel_value - 900, hotel_seen, agent="hotels"):  # would lose 600!
    value, seen = board.read("budget_left")                                         # re-read and retry
    board.write("budget_left", value - 900, seen, agent="hotels")

# Big outputs go in as files; the board only holds a short reference
board.write("report_draft", "artifact://drafts/report-v1.md (4,200 words)", 0, agent="writer")

print("\n".join(board.log))
print("Budget left:", board.read("budget_left")[0])

The hotel agent's first write was refused, because the budget had changed since it read it. It re-read the current value (1,400), subtracted its 900 and wrote 500, which is correct. Without the version check, the board would show 1,100 and the trip would be 600 euros over budget.

This rule is called compare-and-set (or optimistic concurrency): don't lock anything, but refuse writes based on stale reads, and let the writer retry.

Keep shared state simple

The less agents fight over the same entries, the fewer conflicts you have. Three habits help:

  • Ownership: each entry has one agent allowed to write it. Others only read. In the project lesson, each research worker writes only its own topic's findings.
  • Append-only logs: for lists of findings, sources or events, agents add entries and never edit others'. Appends rarely conflict, and you keep a full history.
  • References, not copies: put large outputs, such as a 4,000-word draft or a long table, in a file or store and share a short reference to it. The demo's board holds artifact://drafts/report-v1.md, not the report itself. Anthropic's research system used the same idea: sub-agents saved outputs to files and passed lightweight references, avoiding the telephone game.

Common mistakes

Practice exercise

Write a compare-and-set update, the rule that stops two agents from silently overwriting each other's work.

Your solutionPython
store = {"status": ("drafting", 3)}          # key: (value, version)

def write(key, value, seen_version):
    # If seen_version equals the key's current version, store (value, version + 1)
    # and return True. Otherwise change nothing and return False.
    pass

print(write("status", "reviewing", 3))       # this agent saw version 3
print(write("status", "published", 3))       # this agent also saw version 3: too late
print(store["status"])
Knowledge check

Two agents read version 7 of a shared entry. The first writes successfully. What should happen when the second tries to write based on version 7?

Summary

  • Shared state keeps information many agents need in one place; a blackboard is a shared space agents read and post to.
  • Concurrent writers cause lost updates unless writes are checked.
  • Versioned compare-and-set writes refuse stale updates; the writer re-reads and retries.
  • Keep it simple with ownership, append-only logs and references to large outputs.

Next: the project, a multi-agent research system that puts all of this together.

Finished reading? Tick it off to track your progress.