Skip to content
AgenticAIWiki
  1. Learn
  2. Part 10
  3. 56. Long-term stores
  4. 56.2

Lesson 56.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026

Key-value and relational stores

By the end, you can

  • Use key-value and relational stores for agent memory.
  • Pick exact lookups over similarity where possible.

Key-value stores

prefs:C-2201  -> {"language": "de", "tone": "brief", "units": "metric"}
session:s-881 -> {"order_id": "1042", "step": "awaiting_approval"}

Fast, simple and exact. Good for preferences, session state and small profiles.

Relational databases

SELECT plan, renewal_date FROM accounts WHERE customer_id = 'C-2201';

Good for structured facts with relationships: accounts, orders, tickets. Usually these are your existing systems of record.

Exposing them to agents

Give agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. narrow tools, not raw SQL:

Tool Instead of
get_preferences(user_id) SELECT * FROM ...
set_preference(user_id, key, value) with allowed keys Arbitrary writes
get_order(order_id) with ownership check Open queries

This keeps access safe and predictable (chapter 65), and simpler for the model to use (chapter 34).

Combining stores

A typical agent uses several: key-value for preferences and state, the relational database for account facts, and a vector storeVector database: A database built to store embeddings and find the nearest ones to a query quickly. for past conversation summaries. Each answers a different kind of question.

Key points

  • Key-value stores give fast exact lookups: user id to preferences, session id to state.
  • Relational databases hold structured facts with relationships and are queried precisely.
  • Expose them to the agent through narrow tools, not raw query access.
Hands-on labRuns in the Playground

Wrap a preferences store in narrow tools

Expose a key-value preferences store to an agent through two narrow tools with allowed keys and an ownership check, instead of raw queries.

You will have: get_preferences and set_preference functions, plus a test run showing which calls succeed and which are rejected.

Steps

  1. Read the KV dictionary: keys look like prefs:<user_id>.
  2. Write get_preferences(caller, user_id) that refuses to read another user's preferences.
  3. Write set_preference(caller, user_id, key, value) that only accepts language, tone and units, and writes back to the same user's key.
  4. Run the sample CALLS and print ok or the rejection reason for each.
  5. Print the final KV contents.

Done when

  • Exactly two of the five sample calls are rejected, each with a reason.
  • C-2201's language is updated to de and no other user's row changes.
  • No call can write a key outside the allowed list.

Open in the Playground No API key needed.

Starter code (Python)
KV = {"prefs:C-2201": {"language": "en", "tone": "brief"}, "prefs:C-3300": {"language": "fr"}}
ALLOWED_KEYS = {"language", "tone", "units"}

def get_preferences(caller, user_id):
    # TODO: refuse if caller != user_id; otherwise return a copy of the row (or {})
    return KV.get("prefs:" + user_id, {})

def set_preference(caller, user_id, key, value):
    # TODO: refuse other users and unknown keys; write to "prefs:" + user_id
    return "ok"

CALLS = [
    ("get", "C-2201", "C-2201"),
    ("set", "C-2201", "C-2201", "language", "de"),
    ("set", "C-2201", "C-2201", "is_admin", "yes"),
    ("get", "C-2201", "C-3300"),
    ("set", "C-2201", "C-2201", "units", "metric"),
]
for c in CALLS:
    try:
        out = get_preferences(*c[1:]) if c[0] == "get" else set_preference(*c[1:])
        print(c, "->", out)
    except PermissionError as e:
        print(c, "-> rejected:", e)
print(KV)
Show a solution
KV = {"prefs:C-2201": {"language": "en", "tone": "brief"}, "prefs:C-3300": {"language": "fr"}}
ALLOWED_KEYS = {"language", "tone", "units"}

def get_preferences(caller, user_id):
    if caller != user_id:
        raise PermissionError("cannot read another user's preferences")
    return dict(KV.get("prefs:" + user_id, {}))

def set_preference(caller, user_id, key, value):
    if caller != user_id:
        raise PermissionError("cannot change another user's preferences")
    if key not in ALLOWED_KEYS:
        raise PermissionError("unknown key " + key)
    row = KV.setdefault("prefs:" + user_id, {})
    row[key] = value
    return "ok"

CALLS = [
    ("get", "C-2201", "C-2201"),
    ("set", "C-2201", "C-2201", "language", "de"),
    ("set", "C-2201", "C-2201", "is_admin", "yes"),
    ("get", "C-2201", "C-3300"),
    ("set", "C-2201", "C-2201", "units", "metric"),
]
rejected = 0
for c in CALLS:
    try:
        out = get_preferences(*c[1:]) if c[0] == "get" else set_preference(*c[1:])
        print(c, "->", out)
    except PermissionError as e:
        rejected += 1
        print(c, "-> rejected:", e)
print("rejected calls:", rejected)
print(KV)
Quick checkAn agent needs the user's saved language preference at the start of every chat. Which store fits best?
How sure are you?
Pick an answer and how sure you are