Skip to content

Thread state: what a conversation keeps between runs ​

examples/patterns/thread-state.halo policies

@thread(user_id) gives each user a thread that lasts from one run to the next. A run reads it as context.thread and changes it with Thread.set, Thread.append and Thread.clear. The changes are saved when the run ends: not if it fails, and not while it waits at a yield. Saving applies them to the thread as it is then, so two messages from one user handled at once both land in the history.

Open in the playground and play with it →

bash
halo run examples/patterns/thread-state.halo --arg user_id=ana --arg msg="I'm planning a trip to Lisbon"
halo run examples/patterns/thread-state.halo --arg user_id=ana --arg msg="What should I pack?"
halo thread Chat ana
halo thread Chat ana --reset --yes
halo
// ============================================================
// Thread state: what a conversation keeps between runs
// ============================================================
// `@thread(user_id)` gives each user a thread that lasts from one run to
// the next. A run reads it as `context.thread` and changes it with
// `Thread.set`, `Thread.append` and `Thread.clear`. The changes are saved
// when the run ends: not if it fails, and not while it waits at a yield.
// Saving applies them to the thread as it is then, so two messages from
// one user handled at once both land in the history.
//
// RUN:  halo run examples/patterns/thread-state.halo --arg user_id=ana --arg msg="I'm planning a trip to Lisbon"
//       halo run examples/patterns/thread-state.halo --arg user_id=ana --arg msg="What should I pack?"
// SEE:  halo threads
//       halo thread Chat ana
//       halo thread Chat ana --reset --yes
// ============================================================

policy Brief : MaxWords(120) & MaxSpend(0.02)

agent Assistant [anthropic/claude-sonnet-4-5, temp: 0.4]
  system """
    You're a friendly travel assistant in a chat. You get the recent conversation and a short summary of what you know about this person. Use them so the person never has to repeat themselves, and answer in a few sentences.
  """
  requires Brief

agent Summarizer [anthropic/claude-haiku-4-5, temp: 0.0]
  system """
    You keep a running summary of what we know about a traveler: where they're going, when, with whom, and what they care about. Given the previous summary and the latest exchange, write the new summary in at most three sentences. Keep facts; drop small talk.
  """
  requires Brief

@thread(user_id)
pipeline Chat(user_id: String, msg: String) -> Result:
  msg
    // 1. Answer with what the thread remembers
    |> Assistant.reply("What we know: {{context.thread.summary}}\nRecent messages: {{context.thread.messages}}\nThey say: {{msg}}") as reply

    // 2. Remember the exchange (the last 20 messages) and refresh the summary
    |> Thread.append(messages: ["user: {{msg}}", "assistant: {{reply}}"], keep: 20)
    |> Summarizer.update("Previous summary: {{context.thread.summary}}\nThey said: {{msg}}\nWe answered: {{reply}}") as summary
    |> Thread.set(summary: context.data.summary)
    |> System.Value(context.data.reply)

curl -fsSL https://halolang.com/install.sh | sh