Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 61. Sessions
  4. 61.3

Lesson 61.3 · 3 of 4 · 4 min read · Reviewed 24 Nov 2026

Isolating sessions

By the end, you can

  • Isolate sessions between users, tasks and environments.
  • Avoid leaking context across sessions.

One session per unit of work

  • Per user, per conversation in a chat product.
  • Per ticket in support.
  • Per job in CIContinuous integration (CI): Automatically building and testing code on every change, usually in a hosted pipeline..

A shared session means shared history, and that means one person’s data can appear in another person’s answer.

Store sessions safely

  • Save the session id with its owner and expiry.
  • Before resumingSession resumption: Continuing an earlier session by reloading its saved history or state., check that the requester owns the session.
  • Delete or expire sessions you no longer need, especially if they contain personal dataPII: Personally identifiable information: data that can identify a person, such as name, email, address or ID numbers..

Isolation of tools and files too

Sessions that run tools also need isolated workspaces: separate directories, containers or credentials. An agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. working on customer A’s files shouldn’t be able to read customer B’s.

Key points

  • Each user, ticket or CI job should get its own session.
  • Shared sessions can leak one user's data into another's answers.
  • Store session ids with an owner and an expiry, and check ownership before resuming.
Hands-on labRuns in the Playground

Add an ownership check to resume

Stop one user resuming another user's session, and expire old sessions.

You will have: A resume() function that checks owner and expiry, with a printed table of four access attempts.

Steps

  1. Run the starter: every attempt is allowed, including Bob reading Alice's ticket.
  2. Return 'denied: unknown session' for ids that are not in the store.
  3. Compare the stored owner with the requester, who comes from your login system, not from the chat.
  4. Deny and delete sessions past their expiry.

Done when

  • Only alice -> t-881 is 'ok'.
  • The output shows 'allowed: 1 denied: 3'.
  • The expired session t-882 is no longer in the store.

Open in the Playground No API key needed.

Starter code (Python)
# Isolate sessions: store an owner and an expiry, check both before resuming.
CLOCK = 1000   # a fake clock in minutes, so the lab is deterministic
STORE = {}     # session_id -> {"owner": ..., "expires": ..., "messages": [...]}

def create(session_id, owner, ttl_minutes=60):
    STORE[session_id] = {"owner": owner, "expires": CLOCK + ttl_minutes, "messages": []}

def resume(session_id, requester, now):
    # TODO: deny unknown ids, sessions owned by someone else, and expired sessions
    # TODO: return ("ok", messages) or ("denied: <reason>", None)
    return "ok", STORE.get(session_id, {}).get("messages")

create("t-881", owner="alice")
create("t-882", owner="bob", ttl_minutes=5)
for sid, who, now in [("t-881", "alice", 1010), ("t-881", "bob", 1010), ("t-882", "bob", 1030), ("t-999", "alice", 1010)]:
    status, _ = resume(sid, who, now)
    print(f"{who} -> {sid}: {status}")
Show a solution
CLOCK = 1000
STORE = {}

def create(session_id, owner, ttl_minutes=60):
    STORE[session_id] = {"owner": owner, "expires": CLOCK + ttl_minutes, "messages": []}

def resume(session_id, requester, now):
    s = STORE.get(session_id)
    if s is None:
        return "denied: unknown session", None
    if s["owner"] != requester:
        return "denied: not owner", None
    if now >= s["expires"]:
        del STORE[session_id]          # expired sessions with personal data are removed
        return "denied: expired", None
    return "ok", s["messages"]

create("t-881", owner="alice")
create("t-882", owner="bob", ttl_minutes=5)
results = []
for sid, who, now in [("t-881", "alice", 1010), ("t-881", "bob", 1010), ("t-882", "bob", 1030), ("t-999", "alice", 1010)]:
    status, _ = resume(sid, who, now)
    results.append(status)
    print(f"{who} -> {sid}: {status}")
print("allowed:", results.count("ok"), "denied:", len(results) - results.count("ok"))
print("sessions left in store:", sorted(STORE))
Quick checkA support bot reuses one agent session for all customers to save setup time. What's the main risk?
How sure are you?
Pick an answer and how sure you are