Agent Runtime, Context & Memory
Published
Your bar: explain to an interviewer how an agent runtime orchestrates an LLM loop, why context engineering often matters more than model choice, and how the three memory tiers map to production storage systems. 1
One sentence holds all three modules. Each module unpacks one chip:
An agent is a stateless LLM run by a runtime
that manages context carefully
and stores state in three memory tiers
because the LLM itself remembers nothing
Agent Runtime — the OS around the LLM
The LLM is stateless. Someone has to run the loop, manage tools, and know when to stop. That’s the runtime.
Memory check
- Name four responsibilities the runtime has that the LLM cannot do. → loop orchestration, tool execution, retry/backoff, cost/safety limits
- What does the LLM actually emit when it "calls a tool"? → structured text (tool name + args); the runtime executes the real call
- Closest SWE analogy for the runtime? → web server + queue worker + middleware — not just "a function"
Walk me through the lifecycle of one agent loop iteration from the moment the LLM returns output.
Hit these points: LLM returns text with a tool-call block → runtime parses the output and extracts tool name + args → validates tool name against the allowed list and validates arg schema → executes the real function (file read, API call, DB query) → captures output or error → appends a [tool_result] message to the conversation history → calls LLM again with updated context → repeat until the LLM returns a final answer (no tool call) or max-steps is exceeded and the runtime halts.
How does a runtime prevent an agent from running forever or spending $500?
Hit these points: loop cap (max iterations per session) → token budget per turn so no single call exceeds limits → wall-clock timeout on the entire session → per-session cost tracking with a hard ceiling → circuit breakers on tool failures (max retries + exponential backoff before aborting) → human-in-the-loop interrupt hooks that pause execution and request confirmation before high-risk or high-cost actions.
Context Management — the RAM budget
A 200k-token window sounds huge. A real codebase is millions of tokens. Every coding agent is fighting this constraint every call.
Memory check
- Why can't a coding agent just send the entire repo to the LLM? → token limit; and even if it fit, irrelevant tokens dilute attention and multiply cost
- Name three stages in context assembly. → retrieve (search), rank (score relevance), assemble (pack into window), compress (summarize if needed)
- Why does context quality often beat model quality? → a weaker model with the right context out-performs a stronger model with noise — the model can't reason about what it wasn't given
How Cursor vs Claude Code handle context limits32
| Approach | Cursor | Claude Code |
|---|---|---|
| Indexing | Embeds entire repo at open time | No upfront index — reads on demand |
| Retrieval | Semantic similarity search on embeddings | File read + grep + directory scan via tools |
| Context assembly | Auto-assembled from index results | Agent explicitly reads files it decides are relevant |
| Strength | Fast on large repos, always fresh relevant snippet | Precise — agent reads exactly what matters for the task |
| Weakness | Embedding quality can miss structural/logical relationships | Slower start; reads more tokens to understand project layout |
Why is context management often more important than the model itself?
Hit these points: The model can only reason about what it’s given — wrong or irrelevant context produces wrong answers regardless of model capability. Right context with a smaller model often beats wrong context with the best model. Budget is also a direct function of tokens sent — context selection is the primary cost lever. Finally, context errors are silent: the model doesn’t say “you gave me the wrong files” — it just answers incorrectly based on what it received.
You are building a coding agent. The codebase is 2M tokens but your window is 200k. What is your retrieval strategy?
Hit these points: embed + similarity search for relevant files by function/class/symbol name → supplement with structural context (file tree, import graph, dependency map) → prioritize files the user’s query mentions by name — exact matches first → compress large files by showing only function signatures and docstrings, not bodies → reserve a fixed token budget for tool outputs and conversation history and enforce it → track what you’ve loaded to avoid refetching → instrument retrieval quality so you can measure and improve hit rate over time.
Memory — because the LLM forgets everything
The LLM has no memory. Every durable fact must be stored somewhere else and retrieved deliberately.
Memory check
- What happens to short-term memory when a conversation is reset? → it's gone — it only existed in the context window
- Name a production backend for each memory tier. → short-term: in-memory message list; working: Redis / in-process state; long-term: vector DB (Pinecone/pgvector), SQL DB, key-value store
- What should NOT be stored in long-term memory? → raw conversation transcripts (noisy, expensive to search); redundant/stale facts; PII unless required and compliant
Your support agent needs to remember a customer’s product preferences across sessions. What memory tier handles this and what’s your storage/retrieval design?
Hit these points: long-term memory — persists across sessions by definition → store as structured facts in a relational DB keyed by customer_id (not raw transcripts) → retrieve by customer_id lookup at session start and inject into the system prompt as a short, structured block → update after each interaction where a new preference is expressed — write a derived fact, not the full transcript → version or timestamp facts so stale data can be pruned → avoid PII in long-term store unless you have a compliant data-retention policy for it.
What’s the difference between working memory and context window?
Hit these points: Context window is the physical token budget passed to the LLM each call — it’s a technical constraint of the model API. Working memory is a higher-level concept: the scratchpad state the agent maintains for the current task — plans, intermediate results, tool outputs in progress. Working memory is often what fills the context window for a given turn, but short-term history and long-term retrieved facts also compete for that same budget. The distinction matters: context window is the constraint; working memory is one category of content that consumes it.
Retrieval practice — test the three modules
Q1. The runtime's relationship to the LLM is most like…
Q2. Context management matters because…
Q3. Short-term memory in an agent system is…
Q4. An agent's runtime prevents infinite loops by…
Q5. The main difference between Cursor's and Claude Code's context approach is…
Interview — pick your bar
Answer out loud in ~60s, then reveal. Core = recall · Senior = trade-offs & failure modes · Staff = synthesis under ambiguity · System Design = open design round (a different axis, not a harder level).
What is an agent runtime, and name the things it does that the LLM cannot do for itself?
Define the context window and explain what competes for it on a single agent call.
Name the three memory tiers and what each one is for.
Define memory in an agent system — why is "the agent remembers" a misleading phrase?
Walk me through the lifecycle of one agent loop iteration from the moment the LLM returns output.
[tool_result] message to the conversation history → calls the LLM again with updated context → repeat until the LLM returns a final answer (no tool call) or a guardrail (max-steps, budget, timeout) trips and the runtime halts.Why is context management often a bigger lever than which model you pick?
What belongs in long-term memory and what should never go there?
"A bigger context window means we don't need context engineering anymore." Where does that break?
Design the runtime guardrails so an agent can't loop forever or burn $500 on one task.
Design memory for an agent that must keep meaningful state across sessions and across many users.
Answers are wrong. A teammate wants to spend the quarter upgrading the model. Make the principal-level call.
- Clarify scope: task length, autonomy level, tool surface, latency & cost budget, and the blast radius of a wrong action.
- Runtime loop: how a turn flows (parse → validate → execute tool → append result → re-call) and the stop conditions.
- Guardrails: loop cap, per-turn token budget, session cost ceiling, wall-clock timeout, tool retries/backoff, human-in-the-loop gates.
- Context strategy: how the working context is selected, ranked, assembled and compressed within the budget; what's reserved for the answer.
- Memory tiers: what lives short-term vs working vs long-term, the backend for each, and the write/retention policy.
- Freshness & correctness: live look-ups for volatile/exact data; index invalidation; per-user scoping for isolation.
- Observability & failure modes: log assembled context + cost per call; alarm on limits; define behaviour for missing/stale/conflicting state and budget overflow.
Design the runtime + context strategy for a long-running production agent that may run for hours over a 2M-token codebase with a 200k window.
[tool_result], re-call until done or a guardrail trips → because a long run is unbounded by default, every limit must be explicit: loop cap, per-turn token budget, a cumulative session cost ceiling in real dollars checked before each call, a wall-clock timeout, tool retries with backoff, and loop-detection on repeated identical calls → you can show ~10% of the repo at most, so context selection is the product: retrieve by exact symbol match + semantic similarity, walk the import/dependency graph for structure, re-rank, then pack signatures/docstrings over full bodies with the query-named files first → reserve fixed budget for history and the answer, and compress or summarize old turns as the run grows so history doesn't crowd out fresh context → checkpoint progress (a durable plan/scratchpad) so a long task survives a restart → instrument assembled context + token/cost spend per call, alarm on ceilings, and fail closed with a clear halt rather than silently continuing → name the trade-off: pre-indexed embeddings (fast, can go stale) vs read-on-demand (fresh, more round-trips).Design the memory architecture for a customer-support agent that must recall prior interactions across sessions under a fixed token budget.
customer_id → never persist raw transcripts to long-term: write structured, derived facts so retrieval is cheap and precise → partition the window into fixed slices (system prompt & policy, retrieved facts, recent history, current question, reserved answer) so no source starves the others → on session start, look up the customer's facts by id and inject a compact structured block; pull volatile, exact data (current plan, open tickets, balance) live rather than from stale storage → define the write policy: after an interaction, promote new durable facts to long-term, version/timestamp them, and prune stale ones → scope every read/write to the authenticated customer so you never leak another account's memory → observability: measure "had-the-right-fact" rate and retrieval hit-rate, not just CSAT → failure modes: missing fact → ask or escalate rather than hallucinate; conflicting KB vs customer fact → prefer the customer-specific one; budget overflow → drop lowest-ranked history before identity/policy → name the trade-off: pre-stored facts (fast, can lag) vs live look-ups (fresh, exact, more latency), plus the privacy line that history is always retrieved per-customer.These notes reflect my current understanding and are updated as I learn, build, and discover better explanations.