> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blobhub.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Selecting a History

> What a chat sends from a long conversation: roles, an anchor, a range, kept roles and the window guard, with a rolling summary built from them.

Without a bound, every turn sends the whole history, and a long thread costs more on each turn until the vendor
refuses it. A [`genai.chat`](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat) chooses what it sends from its
`history` with parameters of its own, the history selection: roles, an anchor, a range, kept roles and a fit. Nothing
in the platform knows what a summary is. A rolling summary, a running ledger, a shift handover, a fresh start and a
pinned brief are each a few settings of the same parameters ([Built on the selection](#built-on-the-selection)).

The examples on this page use Halden Systems' release thread, `releases/t2-firmware-2.2`. The release team posts its
questions, build results and sign-offs there, a chat answers them, a listener's receipts land there as notes, and a
second chat keeps a rolling summary of it all ([A rolling summary, step by step](#a-rolling-summary-step-by-step)).
After two weeks it holds several hundred items, and no turn should send them all.

## What a message carries

The selection reads three things on a message:

* **A role, any role.** `metadata.role` is whatever its writer declares. The platform uses `request` (someone should
  act on this), `reply` (an answer; nobody has to act) and `note` (bookkeeping, such as a listener's receipt), and
  reserves `result`. Any other role, such as `summary`, `handover`, `brief` or `ledger`, is kept as written. A message
  read from a thread carries its item's role in `metadata.role`. A message without one is selected by the role it
  would be posted with: `reply` for an `assistant` message or a `user` message holding only `tool_result` blocks, and
  `request` for any other ([the mapping](https://docs.blobhub.io/blob-types/workflow/workflows/component-data-thread.md#the-mapping)).
* **Its id and time,** as `attributes.id` and `attributes.created_at`, when it is read from a thread.
* **What it read, on an answer.** Every answer chat writes records `metadata.read`: `from` and `through`, the ids of
  the first and last history messages its request held, and `count`, how many it held. `from` and `through` are left
  out when that message has no id. It is a record anyone can read, and the anchor, the window guard's estimate and the
  reasoning rule below use it.

## The parameters

| Parameter | Value | Default |
| :- | :- | :- |
| `history_roles` | `list` of `text` | Empty: every role |
| `history_skip_roles` | `list` of `text` | `["note"]` |
| `history_anchor` | `text`, a role | None |
| `history_from` | `integer` | None |
| `history_to` | `integer` | None |
| `history_keep` | `list` of `text` | Empty |
| `history_fit` | `trim` or `fail` | `trim` |

* `history_roles`: send only messages of these roles.
* `history_skip_roles`: never send messages of these roles.
* `history_anchor`: the newest message of this role starts what is sent
  ([An anchor is a hard edge](#an-anchor-is-a-hard-edge)).
* `history_from`: the start of a range over the messages after the anchor's edge, counted as a Python slice counts,
  negative from the end.
* `history_to`: the end of that range, not included: `-6` leaves out the newest six.
* `history_keep`: send messages of these roles even where the anchor or the range leaves them out.
* `history_fit`: what happens when the request would not fit the model's window
  ([The window guard](#the-window-guard)).

Every step reads the history in the order its messages count as posted. A tool call's results count right after the
call, and anything posted to the thread while the call ran, such as a person's question or a listener's note, counts
after them. The anchor and its edge, the range, kept roles, the window guard's cut and the start fixed for the run all
count in that order, so none of them separates a call from its results, and the history is sent in it: the call, its
results, then what was posted meanwhile. `read.from` and `read.through`, and so a summary's `read.through`, name
messages of the request in that order. A result counts with its call only within the 100 messages after the call, and a
call whose result a run does not see moves nothing. The thread itself, its items' order and ids, and what the thread
widget shows do not change.

They apply in this order:

1. **Roles.** A message of a skipped role, or of a role outside `history_roles` when that is set, is not sent. An
   anchor whose role is skipped still marks where the history starts.
2. **The anchor.** The newest message of `history_anchor`'s role is the anchor. Its edge is the message its
   `read.through` names. Every message up to and including the edge is left out, with every other message of the
   anchor's role, and the anchor stands in their place. An anchor without `read`, or whose `read.through` names no
   message of this history, as one a person or a program posted, is its own edge: the history starts at itself. With
   no message of that role, nothing is left out.
3. **The range.** `history_from` and `history_to` slice the messages after the edge that the roles let through. The
   anchor is not counted, and it is sent unless its role is skipped.
4. **Kept roles.** A message of a `history_keep` role that the anchor or the range left out is sent anyway, in the
   history's order, unless its role is skipped.
5. **The fit:** [the window guard](#the-window-guard).

What is sent keeps the history's order. The anchor stands at its edge, after the kept messages from before the edge
and before the messages after it, and `current` follows the last of them.

**Turns.** A turn starts at a `request` and runs to the next one: a question, then the answers, tool calls and tool
results that follow it. Messages after the edge and before its first request form a turn of their own. The range and
the window guard cut only between turns:

* A range whose first message is not a turn's request starts at that turn's request, so a tool result is never sent
  without its call.
* A range whose end falls inside a turn ends before that turn's request, so `history_to: -6` leaves out at least the
  newest six.
* An anchor's edge that falls inside a turn, when the first message sent after it is not a request, moves back to
  before that turn's request; when no request precedes it, the edge moves before the first message sent, and leaves
  nothing out. The turn is sent whole, so the messages of it the anchor's writer did read are sent again. The edge
  never covers a message posted after `read.through`.
* The window guard leaves out whole turns.

A call and its results stay together through all of these. What can still separate them is a result posted more than
100 messages after its call, a run that began before the result was posted, and a role filter or a kept role that
leaves out one of the two: the request then sends the block that has no partner as text, with a warning
([A call goes with its result](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md#a-call-goes-with-its-result)).

## An anchor is a hard edge

An anchor is a hard edge: a message the anchor's writer read is never sent again, even where the range would reach it,
except in a turn the edge fell inside. Over the release thread, whose newest summary read all but the last 30 items,
`history_from: -100` sends the summary and those 30. Only `history_keep` brings a message back across the edge. A flow
that needs everything, an export or an audit, sets no anchor.

**An anchor resumes where its writer stopped reading, not where it was posted.** A summary is posted after the items
it read, and the thread keeps growing while it is written. The items its writer did not read sit before it in the
thread but after its `read.through`, so they are sent after it: the newest six it left out with `history_to: -6`, or a
build report posted while the summary was being written. Position would lose them.

The release thread, read with `history_anchor: summary` when the release manager's next question starts a run (notes,
which the default `history_skip_roles` leaves out, are not shown):

| Thread order | Item | Sent |
| :- | :- | :- |
| 1 to 41 | Questions, answers and reports, summary 1 among them | No: summary 2's edge is item 41 |
| 42 to 47 | The six newest when summary 2 was written, which its writer left out with `history_to: -6` | Yes, after summary 2 |
| 48 | A build report, posted while summary 2 was being written | Yes |
| 49 | Summary 2, role `summary`, whose `read.through` is item 41 | Yes, first: it stands at its edge, item 41 |
| 50 | The answer to item 47, posted in the run that wrote summary 2 | Yes |
| 51 | The release manager's next question, role `request` | Yes, last |

Summary 1 is left out with the rest of the anchor's role. `history_keep: ["brief"]` would bring back the release's
first post if it had the role `brief`, from before the edge, sent before summary 2.

## A start fixed for the run

A chat node's first call in an execution fixes where its history starts: the anchor, the range's first message and
the window guard's cut. Its later calls in that execution start in the same place, so a
[tool loop](https://docs.blobhub.io/blob-types/workflow/workflows/tool-calling.md) never moves them, even when a newer anchor or a post arrives
between two calls. Over a `data.messages` history whose messages carry no ids, the start is kept by position, which
holds while the value only grows at its end, as it does in a tool loop.

`logic.copy` and `thread_messages` select afresh on every call
([One selection, in three places](#one-selection-in-three-places)).

## The window guard

Before each call to a model [the catalog](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-model-language.md#the-catalog)
lists, chat estimates the request against the model's context window. The estimate counts:

* the system message, the tools, the history that is sent, `current`, and the `max_output_tokens` the call uses, 0
  when it is unset;
* four characters a token over each block's JSON text;
* each file at its dialect's estimate: by default 1,600 tokens for an image, and 20, 8 and 2 tokens a KiB for a PDF,
  audio and video;
* in place of characters, the newest answer's recorded `metadata.usage.input_tokens`, when this model wrote that
  answer and its `read.from` and `read.through` are both in the request: it stands for the system message, the tools
  and the messages from `read.from` through `read.through`, and the rest is counted by characters.

What happens when the estimate is over the window:

* **With `trim`**, the default, the oldest whole turn after the anchor is left out, then the next, until the request
  fits. The provider then posts the `component.warning`
  `History trimmed to fit <model>'s context window: <n> messages left out.`, with `attributes.code`
  `history_trimmed` and `attributes.left_out`, and the call goes on. **The system message, the anchor, kept messages,
  `current` and the newest turn are never left out.**
* **When those alone do not fit**, or with `fail` when the request does not fit at all, the call fails before
  anything is sent, with the `component.error`
  `The request needs about <estimate> tokens; <model>'s context window is <window>.`, its `attributes.code`
  `context_exceeded`, `attributes.estimate` and `attributes.window`. Chat takes its `failure` route.
* **A model the catalog does not list** is never trimmed, since its window is unknown; a request too long for it
  meets the vendor's own refusal.

The guard is a last resort, not a policy: a flow that meets it should anchor. Both codes are stored in the event's
attributes whether or not the session logs data
([Events](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md#events)).

## What moving the start costs

The vendors bind reasoning and the prompt cache to the history before them. Stored messages still render the same on
every turn: the platform rewrites nothing, and a selection only starts later
([The same on every turn](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md#the-same-on-every-turn)).

* **Reasoning** goes back on an answer only when its `read.from` is this request's first history message, compared as
  they are, absent matching absent, so over a `data.messages` history without ids it always goes back. Otherwise the
  answer goes back as text, files, tool calls and results, as it does to another vendor
  ([Reasoning](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md#reasoning)).
* **An anchor moves the start once for each new anchor.** Where a
  [prompt cache](https://docs.blobhub.io/blob-types/workflow/workflows/prompt-caching-and-usage.md) is at work, the turn after a new anchor
  writes it anew, and the turns after that read it.
* **A negative `history_from` moves the start on every run,** so such a flow reads no cache across turns and sends
  back no earlier reasoning. The window guard's cut does the same while it trims.

An anchor is the way to bound a long conversation.

## `current` is for this call only

Chat sends `current` after the history and never posts it, so it does not join the conversation: no later turn reads
it from the thread. On a conversation it stays unwired, because the person's post is already the end of the history.
It carries what shapes one answer without joining the conversation: a summarizer's instruction, a judge's rubric, the
passages a search retrieved for this question. A flow with no thread passes its prompt there.

An empty `current`, with no blocks, or only `text` blocks whose text is empty or whitespace, is not sent. Chat answers
whenever `current` is sent, which is why a summarizer posts a summary every time the flow reaches it. Without
`current`, a chat with a `history` answers only when that history is not empty and its newest message is not the
model's own answer; otherwise it calls no model and writes nothing
([What it answers](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md#what-it-answers)).

## One selection, in three places

The same parameters, under three sets of names:

| `genai.chat` | [`logic.copy`](https://docs.blobhub.io/blob-types/workflow/workflows/component-logic-copy) | [`thread_messages(...)`](https://docs.blobhub.io/blob-types/workflow/workflows/component-code#threads) in `logic.code` |
| :- | :- | :- |
| `history_roles` | `roles` | `roles` |
| `history_skip_roles` | `skip_roles` | `skip_roles` |
| `history_anchor` | `anchor` | `anchor` |
| `history_from` | `from` | `from_` |
| `history_to` | `to` | `to` |
| `history_keep` | `keep` | `keep` |
| `history_fit` | none | none |

* **`genai.chat`** selects its `history` and fits it to the model's window.
* **`logic.copy`** selects its `input`, so a flow can hand any component a selected history: a
  `text.render_template`, or a `data.messages` kept for review.
* **`thread_messages(alias, roles=None, skip_roles=None, anchor=None, from_=None, to=None, keep=None)`** takes the
  same names as keyword arguments. The range's start is `from_`, because `from` is a Python keyword. An argument left
  out takes its default, so notes are left out unless `skip_roles=[]` reads them.

When the history is a thread, the read applies the selection as it goes, newest first, and stops at the anchor's
edge, or, without an anchor, at the range's first turn: an anchored history never reads the whole thread. With
`history_keep` set, the whole thread is read. Any other component that reads a
[`data.thread`](https://docs.blobhub.io/blob-types/workflow/workflows/component-data-thread#read) gets the thread whole, notes left out.

## Built on the selection

* **A rolling summary:** a summarizer chat reads with `history_anchor: summary` and `history_to: -6`, has its
  instruction in `current`, and posts its answer with `output_metadata: {role: summary}`; the answering chat reads with
  `history_anchor: summary`. Below, and the gallery's [Launch Pulse](https://blobhub.io/workflow-gallery/launch-pulse)
* **A running ledger:** as a summary, with a `response_schema`: each run reads the last ledger and what followed it,
  and writes the next. Role `ledger`
* **A shift handover:** the shift's last run posts a `handover`; the next shift's chat reads with
  `history_anchor: handover`
* **A fresh start in one thread:** a post with the role `boundary`; chat reads with `history_anchor: boundary` and
  `history_skip_roles: ["note", "boundary"]`
* **A pinned brief:** the task's first post has the role `brief`, and `history_keep: ["brief"]` sends it past every
  anchor and range
* **A sliding window:** `history_from: -20`, at the price in [What moving the start costs](#what-moving-the-start-costs)
* **A router or a classifier:** `history_roles: ["request"]` and `history_from: -1`
* **Notes one chat keeps from another:** a chat posts its scratch work with the role `scratch.planner`, and the other
  chats skip that role
* **An export:** `logic.copy` with no anchor and `skip_roles: []`

## A rolling summary, step by step

The summarizer's input is the previous summary and the items since it, and its output is the next summary. What a
summary holds, and how the old one and the new items combine, is its instruction's to say: rewrite it whole, add a
dated section, or carry counts forward. The platform concatenates nothing.

Halden's release flow runs each time the release team posts in the
[conversation widget](https://docs.blobhub.io/blob-types/workflow/playgrounds/widgets.md). Its three steps:

| Step | Component |
| :- | :- |
| Due? | `logic.code` |
| Summarize | `genai.chat` |
| Answer | `genai.chat` |

* **Due?** runs the program below. Its `due` route leads to Summarize, and its `skip` route to Answer.
* **Summarize** has `history` and `output` wired to the `data.thread` `releases/t2-firmware-2.2`;
  `history_anchor: summary`; `history_to: -6`; `current` holds the instruction, "Fold the previous summary and the
  items since into the next: open blockers, test results and sign-offs."; `output_metadata: {role: summary}`. It goes
  on to Answer.
* **Answer** has `history` and `output` wired to the same `data.thread`; `history_anchor: summary`.

The runs:

1. **The first time,** there is no summary: the summarizer reads every item but the newest six and writes summary 1,
   whose `read.through` names the newest item it read, the seventh-newest when the cut falls between turns.
2. **The answering chat** sends summary 1 and then every item after its `read.through`: the six it left out and
   anything posted since.
3. **The next time,** the summarizer reads summary 1 and the items after its `read.through` but the newest six, and
   writes summary 2. Summary 1 is never sent again.
4. **Whether a summary is due is the flow's to decide,** before the summarizer, as for any component: a chat whose
   `current` is sent answers every time the flow reaches it, so the summarizer posts a summary on every call. Due?
   reads what the next summary would cover: the items after the newest summary's `read.through`, the summary itself
   left out. It routes `due` to the summarizer when they pass a count, and `skip` past it otherwise. Both routes are
   wired, because a route with no connection fails the execution.

Three of the release flow's runs follow, each as its own diagram. The team's post starts every run.

Run 1: the first summary.

```mermaid theme={null}
sequenceDiagram
    participant thread as Thread
    participant gate as Due?
    participant summarizer as Summarize
    participant chat as Answer

    Note over thread: The team posts a question
    gate->>thread: reads the new items
    thread-->>gate: 31, no summary yet
    gate->>summarizer: due
    summarizer->>thread: reads all but the newest six
    summarizer->>thread: posts summary 1
    chat->>thread: reads summary 1 and what follows
    chat->>thread: posts its answer
```

Run 2: the next run, which skips the summarizer.

```mermaid theme={null}
sequenceDiagram
    participant thread as Thread
    participant gate as Due?
    participant chat as Answer

    Note over thread: The team posts a question
    gate->>thread: reads the new items
    thread-->>gate: 8 items
    gate->>chat: skip
    chat->>thread: reads summary 1 and what follows
    chat->>thread: posts its answer
```

A later run: the second summary.

```mermaid theme={null}
sequenceDiagram
    participant thread as Thread
    participant gate as Due?
    participant summarizer as Summarize
    participant chat as Answer

    Note over thread: The team posts a question
    gate->>thread: reads the new items
    thread-->>gate: 30 items
    gate->>summarizer: due
    summarizer->>thread: reads summary 1 and what<br/>follows, but the newest six
    summarizer->>thread: posts summary 2
    chat->>thread: reads summary 2 and what follows
    chat->>thread: posts its answer
```

**The `Due?` gate** is one `logic.code` program. It reads the thread with
[`thread_messages`](https://docs.blobhub.io/blob-types/workflow/workflows/component-code#threads), anchored on `summary` with `summary` and
`note` skipped, so it counts what follows the newest summary's edge and not the summary itself, and routes with
`set_result`:

```python theme={null}
pending = thread_messages("releases/t2-firmware-2.2", anchor="summary", skip_roles=["note", "summary"])
set_result("due" if len(pending["messages"]) >= 30 else "skip")
```

Thirty messages is this example's count. A gate may measure size instead, characters over four against a budget, or
age; it is the flow's rule, not the platform's.

The gallery's [Launch Pulse](https://blobhub.io/workflow-gallery/launch-pulse) summarizes a product launch's feedback
with the same summarizer settings, behind a gate that measures size, and its summaries carry each theme's count forward.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.