Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Memory

Every run so far started from nothing. That is fine for a calculator. It is annoying for anything a person talks to more than once. Tell a travel helper on Monday that you are vegetarian, and on Tuesday it suggests a steakhouse.

This lesson gives an agent long-term memory: facts about one user, kept in a SQLite file, there again after the program restarts.

Memory is per user, and you pick the user

#![allow(unused)]
fn main() {
async fn concierge(db: &Path, user: &str, session: &str) -> Result<BuiltAgent, KitError> {
    Agent::new("concierge")
        .model(MODEL)
        .instructions(
            "You help a customer plan trips. When the customer tells you a lasting \
             preference, save it with remember_fact. Use what you know about the \
             customer in every answer, in at most three sentences.",
        )
        .memory(AgentMemory::new(user, session).sqlite(db))
}

AgentMemory::new(user, session) is the important line. It gives the agent facts about one user and a summary of one session, stored in the file passed to .sqlite(..). It also gives the agent five tools to work with them: remember_fact, forget_fact and recall_facts for facts, and update_session_summary and read_session_summary for the summary.

Notice who chooses user. Your code does, every time it builds the agent. The model never does. It cannot ask for another user's facts, because it never decides whose facts it is looking at. That is the line that keeps your users apart, and it is in your code, not in a prompt.

By default the stored facts are also added to the start of every run, so the agent sees them without having to call recall_facts. Memory needs the knowledge feature.

Monday, then a restart, then Tuesday

The example plays three scenes. On Monday, ada tells the agent two things about herself:

#![allow(unused)]
fn main() {
let monday = concierge(&db, "ada", "monday").await?;
let told = monday
    .run("I am vegetarian and I never take flights before 9 am. Please remember that.")
    .await?;
println!("ada, monday:\n{told}\n");
drop(monday);
}

The instructions tell the agent to save lasting preferences, so it should call remember_fact. Then we drop the agent, which is as close to a restart as one program gets: nothing about Monday is left in memory, only in the file.

On Tuesday we build a brand new agent on the same file and ask about a trip:

#![allow(unused)]
fn main() {
let question = "Plan dinner and the flight for my trip to Lisbon on Friday.";
let tuesday = concierge(&db, "ada", "tuesday").await?;
println!("ada, tuesday:\n{}\n", tuesday.run(question).await?);
}

And then bob asks the same question, on the same file:

#![allow(unused)]
fn main() {
let bob = concierge(&db, "bob", "tuesday").await?;
println!("bob, tuesday:\n{}", bob.run(question).await?);
}

Try it

cargo run -p troupekit --features knowledge --example course_memory

Ada's Tuesday answer should suggest a vegetarian dinner and a flight after 9 am, without being told again. Bob's answer should know none of that. Same agent, same file, same question; the only difference is the user your code passed in.

One thing to watch: saving a fact is the model's decision, made because the instructions ask for it. If the Monday reply does not mention saving anything, read the instructions again with the model's eyes. They are the only reason it calls remember_fact.

Limits on memory

Memory is bounded so one chatty user cannot fill your disk. By default a fact is at most 1 024 bytes, a user has at most 200 facts, a session summary is at most 4 096 bytes, and the facts added to each run take at most 800 estimated tokens. fact_caps(..), max_summary_bytes(..) and preamble_budget_tokens(..) change them.

Next: Knowledge.