LaserData Cloud
Laser SDK

Context

One conversation, assembled.

Context binds messages and working memory to one conversation, then assembles a bounded prompt-ready history. Shared knowledge graphs remain cross-conversation.

Built for

Multi-turn agents, support copilots, session replay

How it works

laser.context(conversation_id) opens a scope bound to one conversation. Message and memory operations inherit that id. The graph accessor stays shared by design, as described below.

  • append(topic, payload) writes a turn to a specific topic under that conversation.
  • fetch(topics, n) reads the last n messages back across the given topics.

For anything beyond "last N," compose a policy:

  • LastN(n) caps by message count.
  • TokenBudget(n) caps by an estimated token count.
  • Chain([...]) composes several policies in sequence - last N messages, then trimmed to fit a token budget, applied in that order.

Context assembly follows an explicit policy. fetch requires a bound. Use fetch_with when you need a composed policy or a full replay.

Python uses keyword arguments. Its fetch, block, and assemble_context methods accept last_n and token_budget. They apply LastN before TokenBudget. Use assemble_context(roles=[...]) for role filtering.

The scope reaches every primitive

context(conversation) also scopes related APIs:

  • block(topics, n) renders the last n messages as one newline-joined, prompt-ready string instead of a message list. All three languages have it.
  • scope.memory(namespace) applies the conversation ID to recall and remember operations. It also renders budgeted blocks and runs consolidation. The unscoped laser.memory(..) handle reads cross-conversation facts.
  • scope.graph(name) returns the shared graph. Apply the graph's conversation(id) filter when a query should only include facts from one conversation.

State rebuilds in Rust. scope.state(topics, bound, init, fold) folds records under a ReplayBound. Bounds include offsets, last N, and full replay. state_with(store, ..) starts from the latest snapshot and replays the remaining records.

Context uses regular VSR log topics. Run bootstrap() once to create the agent topics.

Quick example

const ctx = laser.context(conversation)
await ctx.append(AgentTopic.Commands, utf8("book me an aisle seat"))
await ctx.append(AgentTopic.Responses, utf8("booked, aisle 12"))

const turns = await ctx.fetchWith(
  [AgentTopic.Commands, AgentTopic.Responses],
  new ContextChain([new LastN(20), new TokenBudget(4_000)])
)
for (const turn of turns) {
  console.log(decodeUtf8(turn.payload))
}
let scope = laser.context(conversation);
scope
    .append(
        AgentTopic::Commands,
        "book me an aisle seat".as_bytes(),
    )
    .await?;
scope
    .append(
        AgentTopic::Responses,
        "booked, aisle 12".as_bytes(),
    )
    .await?;

let turns = scope
    .fetch_with(
        vec![AgentTopic::Commands, AgentTopic::Responses],
        Box::new(Chain(vec![
            Box::new(LastN(20)),
            Box::new(TokenBudget::new(4_000)),
        ])),
    )
    .await?;

for turn in &turns {
    println!("{}", String::from_utf8_lossy(&turn.payload));
}
ctx = laser.context(conversation)
await ctx.append(ls.Topics.COMMANDS, b"book me an aisle seat")
await ctx.append(ls.Topics.RESPONSES, b"booked, aisle 12")

turns = await ctx.fetch(
    topics=[ls.Topics.COMMANDS, ls.Topics.RESPONSES],
    last_n=20,
    token_budget=4_000,
)
for turn in turns:
    print(bytes(turn.payload).decode())

Full runnable example: Rust · Python · TypeScript

Key operations

VerbWhat it does
context(conversation_id)Scope everything below to one conversation
append(topic, payload)Write one turn under this conversation
fetch(topics, n)Read the last n messages, default policy
fetchWith(topics, policy)Read using a composed policy (Python: fetch(last_n=, token_budget=))
block(topics, n)The last n messages as one prompt-ready string
LastN(n)Policy: cap by message count (Python: last_n=)
TokenBudget(n)Policy: cap by estimated token count (Python: token_budget=)
Chain([...])Compose policies in sequence (Python: passing both keywords)
scope.memory(ns) / scope.graph(name)This conversation's memory, and the shared graph, from one scope
state(topics, bound, init, fold)Fold the conversation's log into in-memory state (Rust)
state_with(store, ..)The same fold seeded from a snapshot, replaying only the tail (Rust)

Running it

Context runs on every VSR target. It does not require laser-plane.

On this page