Agentic Architectures

Catalogue / Memory

Recursive Language Model

The input is a variable; code slices it for fresh model calls

Shape
loop
Model calls
root: 1 per iteration (cap 20) + sub-calls made by the code (cap 50)
Latency
high
Complexity
high

Also known as RLM, Context as environment

1

What if the input is far too large for the model to read?

The long input is never placed in a prompt. It is stored as a variable in a code environment. A root model sees only the query and a few facts about the variable. It writes code that searches and slices it, and that calls fresh model instances on the slices. Their results come back as variables.

You may need it if

  • One request comes with a log, a dump or a set of files many times larger than the context window.
  • Most of the input is irrelevant to the question, but nobody knows in advance which part matters.
  • Answers degrade as the prompt grows, well before the window is full.
  • Building a search index for an input that will be queried once is not worth the work.
2

When is treating the input as a variable not worth it?

Use it when

  • The input for one request is several times larger than the context window.
  • Code can narrow it down, with a search, a filter or a split, before any model reads a part of it.
  • You have a sandbox in which code written by a model can run safely.
  • The answer may need many parts of the input combined, not one passage looked up.

Avoid it when

  • The input is short. One direct reading costs one call. Here it costs code, iterations and sub-calls for the same text.
  • The same corpus will be queried many times. Index it once, then let each question fetch from the index.
    Use Agentic RAG instead
  • Every part of the input gets the same treatment, such as summarising each chapter. A fixed split and merge needs no code loop.
    Use Map-Reduce instead
  • What grows is a conversation history across sessions, not the input of one request.
    Use Tiered Memory instead
  • The cost or delay of a request must be known in advance. The code decides how many sub-calls run, and each one blocks the next.

What it costs

Model calls
root: 1 per iteration (cap 20) + sub-calls made by the code (cap 50)
Latency
high
Complexity
high
3

How does the model work on text it never reads?

A loop between a root model and a code environment. The input is loaded as a variable. The root writes a code cell, the environment runs it, and the code may call fresh sub-models on slices. The root sees a short print-out, writes the next cell, and ends by naming its answer or the variable that holds it.

writes a codecellcode calls amodel on asliceresult returnedto the codetruncatedprint-outnames its finalanswerQuery and input: A question or task, and the long input it is about: a log, a set of files, a large report.Query andinputInput as variable: The whole input held as a string in the code environment, with the variables the cells create.Input asvariableRoot model: Sees the query, a few facts about the input and short print-outs. Writes one code cell per turn.Root modelCode environment: Runs each cell in a sandbox, keeps variables between cells and returns a truncated print-out.CodeenvironmentSub-model calls: Fresh model instances called from the code. Each reads one slice and one instruction, nothing else.Sub-modelcallsAnswer: The final answer, given directly or read from a variable, with the iterations and sub-calls used.Answer
  1. Step 1 of 6

    The input becomes a variable

    Someone asks which deployment started a wave of timeout errors, and attaches a month of server logs, about fifty times the context window. The logs are loaded as one string variable. No model receives them.

  2. Step 2 of 6

    The root model sees the query only

    The root model gets the question, the size of the variable and its first few lines. Its first cell prints the line format and counts the lines that contain "timeout" for each day.

  3. Step 3 of 6

    A short print-out comes back

    The environment runs the cell. The root sees only the start of what was printed. The counts show a jump on the 14th. Its next cell cuts out the hours around that jump and splits them into twelve slices.

  4. Step 4 of 6

    The code calls fresh models on slices

    Inside a loop, the cell calls a sub-model on each slice and asks which deployments and which failing services it mentions. Each sub-model reads one slice and that instruction. It knows nothing of the rest.

  5. Step 5 of 6

    Results stay in variables

    The twelve results are stored in a list. The root is shown a few lines of it, not the whole. That keeps its own context small, and it can still combine the list in code or pass it to one more sub-model.

  6. Step 6 of 6

    The root names its answer

    A last cell builds the answer in a variable: the deployment at 09:40 on the 14th and the three services affected. The root ends the run by naming that variable. After 20 cells it would have been told to finish with what it has.

What each part does
Query and input
A question or task, and the long input it is about: a log, a set of files, a large report.
Input as variable
The whole input held as a string in the code environment, with the variables the cells create.
Root model
Sees the query, a few facts about the input and short print-outs. Writes one code cell per turn.
Code environment
Runs each cell in a sandbox, keeps variables between cells and returns a truncated print-out.
Sub-model calls
Fresh model instances called from the code. Each reads one slice and one instruction, nothing else.
Answer
The final answer, given directly or read from a variable, with the iterations and sub-calls used.
  • Query and input → Input as variable: input loaded as a variable
  • Query and input → Root model: query and facts about the input
  • Root model → Code environment: writes a code cell
  • Input as variable → Code environment: search, slice
  • Code environment → Input as variable: results kept as variables
  • Code environment → Sub-model calls: code calls a model on a slice
  • Sub-model calls → Code environment: result returned to the code
  • Code environment → Root model: truncated print-out
  • Root model → Answer: names its final answer

Where it connects

Entry
query
QuestionTaskDocument

A question or task, plus the long input it is about, as text too large for one window. The input is loaded as a variable and never placed in a prompt.

Exit
answer
Answer

When: The root model names its final answer or the variable that holds it, or the iteration cap is reached.

The answer, the number of iterations and sub-calls used, and a flag when the run ended at the cap instead of on the root model's decision.

Working memory for one run
context
Document

The full input as a string, plus the intermediate variables created by the code. Only code touches it. Discarded when the run ends.

4

What do I ask my assistant to set up?

The agents to launch

Root model

You answer a query about a long input that you cannot see. It is the variable `context` in a code environment. Each turn, write one cell: inspect, search or slice the variable, and call the sub-model function on the slices worth reading. Keep results in variables and print little. To finish, give the answer or name the variable that holds it.

Sees: The query, the size and first lines of the input, its own cells and the first characters of each print-out. Never the input itself.

Sub-model, one fresh instance per call made by the code, at most 50 per run

You receive one instruction and one passage. Answer the instruction using only the passage. If the passage does not contain what is asked, say so in one line. Do not guess what the rest of the document says.

Sees: The prompt built by the code, an instruction and one slice. Not the query, not the other slices, not the root model's history.

A prompt you can paste

Works with any assistant that can launch sub-agents. It contains the agents, the procedure and the stop rule above.

Framework-agnostic prompt
You are the orchestrator of a multi-agent workflow that follows the "Recursive Language Model" architecture.
Launch every agent listed below as a separate sub-agent with its own context. Give each agent only the context stated for it. Do not do an agent's work yourself. If you cannot launch sub-agents, play each agent in a clearly separated turn and never let it use what is withheld from it.

# Task
[Describe your task here]

# Recursive Language Model
The input is a variable; code slices it for fresh model calls.
Input: the task above. It must provide: A question or task, plus the long input it is about, as text too large for one window. The input is loaded as a variable and never placed in a prompt. If something essential is missing, ask for it before starting.

Agents to launch:
- Root model: You answer a query about a long input that you cannot see. It is the variable `context` in a code environment. Each turn, write one cell: inspect, search or slice the variable, and call the sub-model function on the slices worth reading. Keep results in variables and print little. To finish, give the answer or name the variable that holds it.
  Context for this agent: The query, the size and first lines of the input, its own cells and the first characters of each print-out. Never the input itself.
- Sub-model (one fresh instance per call made by the code, at most 50 per run): You receive one instruction and one passage. Answer the instruction using only the passage. If the passage does not contain what is asked, say so in one line. Do not guess what the rest of the document says.
  Context for this agent: The prompt built by the code, an instruction and one slice. Not the query, not the other slices, not the root model's history.

Procedure:
1. Start a sandboxed code environment. Load the whole input into it as one variable. Do not put the input in any prompt.
2. Add a function to the environment that sends a prompt to a fresh sub-model and returns its text. Count its calls.
3. Give the root model the query, the length of the variable and its first lines. Ask for one code cell.
4. Run the cell. Keep every variable for the next cell. Return to the root model only the first 1,500 characters printed, with the total length.
5. Repeat until the root model gives its answer or names a variable. Read the answer from that variable if it named one.
6. After 20 cells, or once 50 sub-calls were made, tell the root model to finish with what the variables hold and mark the answer as incomplete.

Stop rule: Stop when the root model gives a final answer or names the variable that holds it. Stop also after 20 cells; sub-calls are refused after the 50th. Sub-models do not call further sub-models.

Exit:
- `answer`, when: The root model names its final answer or the variable that holds it, or the iteration cap is reached. Deliver: The answer, the number of iterations and sub-calls used, and a flag when the run ended at the cap instead of on the root model's decision.

# Final deliverable
The answer, then the number of cells run and sub-calls made, and one line stating whether the root model finished by itself or was stopped at the cap.

The same thing in code

LangGraph, python, package langgraph
import asyncio
from typing import Any, TypedDict

from langchain.chat_models import init_chat_model
from langgraph.graph import END, START, StateGraph
from pydantic import BaseModel

MAX_ITERATIONS, MAX_SUBCALLS, PREVIEW = 20, 50, 1500  # fixed before the run
ROOT = ("You answer a query about a long input you cannot see. It is the string variable `context` in a "
        "Python environment. Each turn, write one cell: inspect, search or slice the variable, and call "
        "llm_query(prompt) to have a fresh model read one slice. Keep results in variables and print "
        "little: you only see the start of what a cell prints. To finish, set final to the answer or "
        "final_var to the name of the variable that holds it.")
SUB = "Answer the instruction using only the passage given. If the passage does not contain it, say so in one line."

model = init_chat_model("anthropic:claude-sonnet-5-5")  # sub-calls may use a smaller model


class Step(BaseModel):
    code: str  # one Python cell, or "" when finishing
    final: str  # the answer, or ""
    final_var: str  # the variable that holds the answer, or ""


class State(TypedDict):
    query: str
    env: dict[str, Any]  # the code environment: the input, llm_query and the variables cells create
    history: list[str]  # cells and truncated print-outs: all the root model ever sees
    code: str
    answer: str
    done: bool
    complete: bool


def make_env(context: str) -> dict[str, Any]:
    loop, printed, calls = asyncio.get_running_loop(), [], [0]

    def llm_query(prompt: str) -> str:
        # a fresh call that sees this prompt only; it blocks the cell, as the code expects
        calls[0] += 1
        if calls[0] > MAX_SUBCALLS:
            return "Sub-call limit reached. Finish with what the variables hold."
        return asyncio.run_coroutine_threadsafe(model.ainvoke(f"{SUB}\n\n{prompt}"), loop).result().text

    def show(*args: Any) -> None:
        printed.append(" ".join(str(a) for a in args))

    return {"context": context, "llm_query": llm_query, "print": show, "_printed": printed, "_calls": calls}


def run_cell(code: str, env: dict[str, Any]) -> str:
    # replace exec with a real sandbox (container, no network): a model wrote this code
    env["_printed"].clear()
    try:
        exec(code, env)
    except Exception as e:
        env["_printed"].append(f"{type(e).__name__}: {e}")
    out = "\n".join(env["_printed"])
    return f"{out[:PREVIEW]}\n[{len(out)} characters printed, at most {PREVIEW} shown]"


async def root(state: State) -> dict:
    context, last = state["env"]["context"], len(state["history"]) >= MAX_ITERATIONS
    prompt = (f"{ROOT}\n\nQuery: {state['query']}\n`context` has {len(context)} characters and starts with "
              f"{context[:200]!r}\n\nCells so far:\n" + "\n".join(state["history"]))
    if last:
        prompt += "\n\nIteration limit reached. Finish now with final or final_var."
    step = Step.model_validate(await model.with_structured_output(Step).ainvoke(prompt))
    if step.final_var or step.final or last:
        answer = str(state["env"].get(step.final_var, "")) if step.final_var else step.final
        return {"answer": answer, "done": True, "complete": not last}
    return {"code": step.code}


async def cell(state: State) -> dict:
    # the cell runs in a worker thread, so blocking sub-calls can use the event loop
    output = await asyncio.to_thread(run_cell, state["code"], state["env"])
    return {"history": state["history"] + [f">>> {state['code']}\n{output}"]}


builder = StateGraph(State)
builder.add_node("root", root)
builder.add_node("cell", cell)
builder.add_edge(START, "root")
builder.add_conditional_edges("root", lambda s: END if s["done"] else "cell", ["cell", END])
builder.add_edge("cell", "root")
graph = builder.compile()


async def run(task: str, context: str) -> dict:
    start: State = {"query": task, "env": make_env(context), "history": [], "code": "", "answer": "", "done": False, "complete": False}
    s = await graph.ainvoke(start, {"recursion_limit": 2 * MAX_ITERATIONS + 10})
    return {"answer": s["answer"], "iterations": len(s["history"]), "subcalls": min(s["env"]["_calls"][0], MAX_SUBCALLS), "complete": s["complete"]}

exec is a placeholder: run the cells in a sandbox with no network. The input lives in env and never enters a prompt; only 200 characters of it are shown to the root model.

Claude Agent SDK, python, package claude-agent-sdk
import asyncio
from typing import Any

from claude_agent_sdk import ClaudeAgentOptions, ResultMessage, query
from pydantic import BaseModel

MAX_ITERATIONS, MAX_SUBCALLS, PREVIEW = 20, 50, 1500  # fixed before the run
ROOT = ("You answer a query about a long input you cannot see. It is the string variable `context` in a "
        "Python environment. Each turn, write one cell: inspect, search or slice the variable, and call "
        "llm_query(prompt) to have a fresh model read one slice. Keep results in variables and print "
        "little: you only see the start of what a cell prints. To finish, set final to the answer or "
        "final_var to the name of the variable that holds it.")
SUB = "Answer the instruction using only the passage given. If the passage does not contain it, say so in one line."


class Step(BaseModel):
    code: str  # one Python cell, or "" when finishing
    final: str  # the answer, or ""
    final_var: str  # the variable that holds the answer, or ""


async def ask(prompt: str, options: ClaudeAgentOptions) -> ResultMessage:
    # one query per call: a new session, so a sub-model never sees the root model's history
    async for msg in query(prompt=prompt, options=options):
        if isinstance(msg, ResultMessage):
            return msg
    raise RuntimeError("no result")


async def sub_model(prompt: str) -> str:
    return (await ask(prompt, ClaudeAgentOptions(system_prompt=SUB))).result or ""


def make_env(context: str) -> dict[str, Any]:
    loop, printed, calls = asyncio.get_running_loop(), [], [0]

    def llm_query(prompt: str) -> str:
        # blocks the cell, as the code expects; the call itself runs on the event loop
        calls[0] += 1
        if calls[0] > MAX_SUBCALLS:
            return "Sub-call limit reached. Finish with what the variables hold."
        return asyncio.run_coroutine_threadsafe(sub_model(prompt), loop).result()

    def show(*args: Any) -> None:
        printed.append(" ".join(str(a) for a in args))

    return {"context": context, "llm_query": llm_query, "print": show, "_printed": printed, "_calls": calls}


def run_cell(code: str, env: dict[str, Any]) -> str:
    # replace exec with a real sandbox (container, no network): a model wrote this code
    env["_printed"].clear()
    try:
        exec(code, env)
    except Exception as e:
        env["_printed"].append(f"{type(e).__name__}: {e}")
    out = "\n".join(env["_printed"])
    return f"{out[:PREVIEW]}\n[{len(out)} characters printed, at most {PREVIEW} shown]"


async def run(task: str, context: str) -> dict:
    env = make_env(context)  # the input lives here and never enters a prompt
    history: list[str] = []  # cells and truncated print-outs: all the root model ever sees
    root_options = ClaudeAgentOptions(
        system_prompt=ROOT,
        output_format={"type": "json_schema", "schema": Step.model_json_schema()},
    )
    while True:
        last = len(history) >= MAX_ITERATIONS
        prompt = (f"Query: {task}\n`context` has {len(context)} characters and starts with {context[:200]!r}\n\n"
                  "Cells so far:\n" + "\n".join(history))
        if last:
            prompt += "\n\nIteration limit reached. Finish now with final or final_var."
        step = Step.model_validate((await ask(prompt, root_options)).structured_output)
        if step.final_var or step.final or last:
            answer = str(env.get(step.final_var, "")) if step.final_var else step.final
            return {"answer": answer, "iterations": len(history), "subcalls": min(env["_calls"][0], MAX_SUBCALLS), "complete": not last}
        # the cell runs in a worker thread, so blocking sub-calls can use the event loop
        output = await asyncio.to_thread(run_cell, step.code, env)
        history.append(f">>> {step.code}\n{output}")

The variable must outlive each cell, so the environment is kept in this process instead of the SDK's own Bash tool. exec is a placeholder for a sandbox.

OpenAI Agents SDK, python, package openai-agents
import asyncio
from typing import Any

from agents import Agent, Runner
from pydantic import BaseModel

MAX_ITERATIONS, MAX_SUBCALLS, PREVIEW = 20, 50, 1500  # fixed before the run


class Step(BaseModel):
    code: str  # one Python cell, or "" when finishing
    final: str  # the answer, or ""
    final_var: str  # the variable that holds the answer, or ""


root = Agent(
    name="Root model",
    instructions="You answer a query about a long input you cannot see. It is the string variable `context` in "
    "a Python environment. Each turn, write one cell: inspect, search or slice the variable, and call "
    "llm_query(prompt) to have a fresh model read one slice. Keep results in variables and print little: "
    "you only see the start of what a cell prints. To finish, set final to the answer or final_var to the "
    "name of the variable that holds it.",
    output_type=Step,
)
sub = Agent(  # may run on a smaller model
    name="Sub-model",
    instructions="Answer the instruction using only the passage given. If the passage does not contain it, "
    "say so in one line.",
)


async def sub_model(prompt: str) -> str:
    return str((await Runner.run(sub, prompt)).final_output)  # a fresh run: this prompt only


def make_env(context: str) -> dict[str, Any]:
    loop, printed, calls = asyncio.get_running_loop(), [], [0]

    def llm_query(prompt: str) -> str:
        # blocks the cell, as the code expects; the call itself runs on the event loop
        calls[0] += 1
        if calls[0] > MAX_SUBCALLS:
            return "Sub-call limit reached. Finish with what the variables hold."
        return asyncio.run_coroutine_threadsafe(sub_model(prompt), loop).result()

    def show(*args: Any) -> None:
        printed.append(" ".join(str(a) for a in args))

    return {"context": context, "llm_query": llm_query, "print": show, "_printed": printed, "_calls": calls}


def run_cell(code: str, env: dict[str, Any]) -> str:
    # replace exec with a real sandbox (container, no network): a model wrote this code
    env["_printed"].clear()
    try:
        exec(code, env)
    except Exception as e:
        env["_printed"].append(f"{type(e).__name__}: {e}")
    out = "\n".join(env["_printed"])
    return f"{out[:PREVIEW]}\n[{len(out)} characters printed, at most {PREVIEW} shown]"


async def run(task: str, context: str) -> dict:
    env = make_env(context)  # the input lives here and never enters a prompt
    history: list[str] = []  # cells and truncated print-outs: all the root model ever sees
    while True:
        last = len(history) >= MAX_ITERATIONS
        prompt = (f"Query: {task}\n`context` has {len(context)} characters and starts with {context[:200]!r}\n\n"
                  "Cells so far:\n" + "\n".join(history))
        if last:
            prompt += "\n\nIteration limit reached. Finish now with final or final_var."
        step = (await Runner.run(root, prompt)).final_output_as(Step)
        if step.final_var or step.final or last:
            answer = str(env.get(step.final_var, "")) if step.final_var else step.final
            return {"answer": answer, "iterations": len(history), "subcalls": min(env["_calls"][0], MAX_SUBCALLS), "complete": not last}
        # the cell runs in a worker thread, so blocking sub-calls can use the event loop
        output = await asyncio.to_thread(run_cell, step.code, env)
        history.append(f">>> {step.code}\n{output}")

exec is a placeholder: run the cells in a sandbox with no network. Each root turn is a fresh run that gets the history as text, so the input never enters a prompt.

Vercel AI SDK, typescript, package ai
import { anthropic } from '@ai-sdk/anthropic';
import { generateText, Output } from 'ai';
import { z } from 'zod';

const [MAX_ITERATIONS, MAX_SUBCALLS, PREVIEW] = [20, 50, 1500]; // fixed before the run
const model = anthropic('claude-sonnet-5-5'); // sub-calls may use a smaller model
const ROOT =
  'You answer a query about a long input you cannot see. It is the string env.context in a JavaScript ' +
  'environment. Each turn, write the body of one async cell: inspect, search or slice env.context, and ' +
  'call await env.llmQuery(prompt) to have a fresh model read one slice. Keep results in env.vars and ' +
  'print little with env.print: you only see the start of what a cell prints. To finish, set final to ' +
  'the answer or finalVar to the key of env.vars that holds it.';
const SUB = 'Answer the instruction using only the passage given. If the passage does not contain it, say so in one line.';

const stepSchema = z.object({
  code: z.string().describe('the body of one async cell, or "" when finishing'),
  final: z.string().describe('the answer, or ""'),
  finalVar: z.string().describe('the key of env.vars that holds the answer, or ""'),
});

type Env = {
  context: string; // the input lives here and never enters a prompt
  vars: Record<string, unknown>; // what the cells keep between turns
  llmQuery: (prompt: string) => Promise<string>;
  print: (...args: unknown[]) => void;
};

export async function run(task: string, context: string) {
  const printed: string[] = [];
  const history: string[] = []; // cells and truncated print-outs: all the root model ever sees
  let subcalls = 0;
  const env: Env = {
    context,
    vars: {},
    llmQuery: async (prompt) => {
      // a fresh call that sees this prompt only
      if (subcalls >= MAX_SUBCALLS) return 'Sub-call limit reached. Finish with what the variables hold.';
      subcalls++;
      const { text } = await generateText({ model, instructions: SUB, prompt });
      return text;
    },
    print: (...args) => { printed.push(args.map(String).join(' ')); },
  };

  for (;;) {
    const last = history.length >= MAX_ITERATIONS;
    const { output: step } = await generateText({
      model,
      instructions: ROOT,
      prompt:
        `Query: ${task}\nenv.context has ${context.length} characters and starts with ` +
        `${JSON.stringify(context.slice(0, 200))}\n\nCells so far:\n${history.join('\n')}` +
        (last ? '\n\nIteration limit reached. Finish now with final or finalVar.' : ''),
      output: Output.object({ schema: stepSchema }),
    });
    if (step.finalVar || step.final || last) {
      const answer = step.finalVar ? String(env.vars[step.finalVar] ?? '') : step.final;
      return { answer, iterations: history.length, subcalls, complete: !last };
    }
    printed.length = 0;
    try {
      // replace with a real sandbox (isolate, container, no network): a model wrote this code
      const cell = new Function('env', `return (async () => {\n${step.code}\n})();`);
      await cell(env);
    } catch (error) {
      printed.push(String(error));
    }
    const out = printed.join('\n');
    history.push(`>>> ${step.code}\n${out.slice(0, PREVIEW)}\n[${out.length} characters printed, at most ${PREVIEW} shown]`);
  }
}

Cells are JavaScript here. new Function is a placeholder: run them in an isolate or a container with no network.

Snippets are generated when the site is built and type-checked against the SDK versions listed in the repository. Treat them as a starting skeleton.

What goes wrong

  • The code is written by a model and runs on your machine. Use a sandbox with no network and no access to your files.
  • Printing whole slices brings the input back into the root model's context. Truncate every print-out.
  • A loop that calls a sub-model on each line makes thousands of calls. Count sub-calls and refuse them past the cap.
  • A sub-model that sees one slice cannot tell that the answer is elsewhere. Have it say when the slice does not contain it.
  • Slices cut in the middle of a record lose it. Split on natural boundaries such as lines, files or sections.

Chain it

Its result can feed

Source