State and Messages

In shortHow AgentState holds conversation history, how Message carries multimodal content blocks, and how add_messages and custom state subclasses update state.

  • 6 min read
  • 8 sections
  • Updated
  • v1.0.0
  • Markdown

Every node in a graph receives state and returns updates to it. AgentState is the default state class and holds the conversation history. Message is the unit of that history: one turn, made of typed content blocks such as text, images, tool calls and tool results. This page explains how both work and how updates merge.

AgentState

AgentState is a Pydantic model with three built-in fields: the message history, an optional summary of trimmed history, and internal execution metadata. You read context in your nodes and extend the class with your own fields.

Field Type Description
context list[Message] Conversation history. New messages are merged in by the add_messages reducer.
context_summary str | None Summary of messages a context manager removed. None until a summarizing manager runs.
execution_meta ExecMeta Runtime metadata (an ExecutionState): current node, step counter, status, interrupt details, thread id and stop request.

The context field is what the model sees on each turn, so your agent has access to every earlier turn unless a context manager trims it. context_summary keeps a condensed record of removed turns. execution_meta belongs to the runtime. To check run status from a node, use state.is_running(), state.is_interrupted() or state.is_stopped_requested(); see the state reference.

The add_messages reducer

The add_messages reducer decides how a returned list of messages merges into context: it appends them, never replaces.

Two rules apply:

  • A message whose message_id already exists in context is skipped, so re-sending the same message does not create a duplicate.
  • Messages with delta=True (partial streaming chunks) are never stored. Only complete messages enter the history.

The same module exports other reducers (replace_messages, append_items, replace_value, remove_tool_messages), all importable from tenxgraph.core.state. See Use custom state for how to apply them to your own fields.

Custom state

Subclass AgentState to carry application data alongside the conversation. Every node then receives your subclass and can read or write the extra fields.

Pass the class (or an instance, to set initial values) to StateGraph. You can also update custom fields from outside the graph. The state key of the input dict merges the listed fields into the existing state; fields you omit are left alone, and context, context_summary and execution_meta cannot be set this way.

Python
from tenxgraph.core.state import AgentState, Message
from tenxgraph.core.graph import StateGraph
from tenxgraph.utils.constants import END, START

class MyState(AgentState):
    selected_city: str = ""

def reply(state: MyState) -> list[Message]:
    # Read a custom field and append one assistant message to context
    text = f"Selected city: {state.selected_city or 'none'}"
    return [Message.text_message(text, role="assistant")]

graph = StateGraph(MyState)
graph.add_node("REPLY", reply)
graph.add_edge(START, "REPLY")
graph.add_edge("REPLY", END)
app = graph.compile()

result = app.invoke(
    {
        "messages": [Message.text_message("Hi")],
        "state": {"selected_city": "Paris"},  # merged into MyState
    },
    config={"thread_id": "demo"},
)

Message

A Message is one turn in a conversation: a role, a list of content blocks, and metadata. The role says who produced it, and the blocks hold the actual content, so one message can mix text, images and tool calls.

Field Meaning
message_id Generated automatically unless you pass one. The reducer uses it to avoid duplicates.
role "user", "assistant", "system" or "tool".
content A sequence of content blocks.
delta True for a partial streaming message. Delta messages are not stored in context. A delta carries only the new piece of reasoning or text as a block; the final message carries the full reasoning block.
tools_calls Raw tool-call dicts from the model. Tool calls also appear as ToolCallBlock entries in content.
timestamp UNIX timestamp, set at creation.
usages Token counts, when the provider returned them.
raw The provider-native response, when kept.
parsed_content Structured output parsed from the response, when an output schema is used.

Create messages

Use the factory methods for common cases and build a Message directly when you need several blocks.

Python
from tenxgraph.core.state import (
    ImageBlock,
    MediaRef,
    Message,
    TextBlock,
    ToolResultBlock,
)

# Plain text user message
msg = Message.text_message("Hello!")

# Tool result message, linked to the call by call_id
msg = Message.tool_message(
    content=[ToolResultBlock(call_id="call_123", output="Sunny, 22C", status="completed")],
)

# Multimodal message built from explicit blocks
msg = Message(
    role="user",
    content=[
        TextBlock(text="What is this?"),
        ImageBlock(
            media=MediaRef(kind="url", url="https://example.com/img.png", mime_type="image/png")
        ),
    ],
)

Message also has image_message, multimodal_message and from_file constructors. See the Message reference for their parameters.

Extract text

msg.text() returns the concatenated text of the message’s TextBlock entries and the output of its ToolResultBlock entries. Other block types are ignored.

Python
text = msg.text()

Track token usage

When the provider returns usage data, it is stored on msg.usages as a TokenUsages model, so you can measure cost per turn. See TokenUsages for the fields.

Content blocks

A message carries its content as typed blocks, one per kind of content. All block classes are importable from tenxgraph.core.state, and the Message reference lists every field.

Block Holds
TextBlock Text, plus optional annotation references such as citations.
ImageBlock, AudioBlock, VideoBlock Media referenced through a MediaRef, with metadata.
DocumentBlock A document such as a PDF, referenced through a MediaRef.
DataBlock Generic binary data.
ToolCallBlock A model request to call a tool: id, name and args.
ToolResultBlock The output of a tool call, linked by call_id, with is_error and status.
ReasoningBlock Reasoning content from models that expose it. Read it from content; a message has no separate reasoning field. Converters order blocks reasoning, text, media, tool calls.
ErrorBlock An error recorded in the conversation.
AnnotationBlock Annotation data attached to content.

Media blocks point at the data through MediaRef, whose kind is "url", "file_id" or "data". Prefer a URL or provider file id over inline base64 for large payloads. Media and files covers how references are stored and resolved.

ToolResult: tools that update state

A tool normally returns a value that goes back to the model. When it must also change state, return a ToolResult instead. Its message is what the model sees, and its state dict is merged into the graph state.

Python
from tenxgraph.core.state import AgentState, ToolResult

class MyState(AgentState):
    selected_city: str = ""

def select_city(city: str) -> ToolResult:
    """Set the currently selected city in the workflow."""
    return ToolResult(
        message=f"City updated to '{city}'.",  # returned to the model
        state={"selected_city": city},          # updates MyState.selected_city
    )
Parameter Type Default Description
message Any None Text returned to the model as the tool result. content= is accepted as an alias.
state dict[str, Any] | None None Field names mapped to new values. Fields not listed are unchanged.
is_error bool False Marks the result as a failed tool call.

Use this when a tool call should drive later routing, such as a selected filter or a chosen record. If the tool only produces information, return a plain value. Agents and tools explains how tool calls are dispatched.

How context accumulates across turns

Each completed turn appends to context. After a few exchanges the list reads like a transcript, including the tool call and its result.

Python
[
    Message(role="user", content=[TextBlock(text="What is 2 + 2?")]),
    Message(role="assistant", content=[TextBlock(text="4.")]),
    Message(role="user", content=[TextBlock(text="What is the weather in Tokyo?")]),
    Message(
        role="assistant",
        content=[ToolCallBlock(id="c1", name="get_weather", args={"location": "Tokyo"})],
    ),
    Message(role="tool", content=[ToolResultBlock(call_id="c1", output="Cloudy, 18C", status="completed")]),
    Message(role="assistant", content=[TextBlock(text="It is cloudy and 18C in Tokyo.")]),
]

With a checkpointer, this list is saved under the thread id and restored when the thread resumes, which is why a conversation on the same thread feels continuous. See Checkpointing and threads.

Keep context within the model window

To keep long threads inside the model window, register a context manager and set Agent(trim_context=True); see Context management. For facts that should outlive a conversation, see Long-term memory.

Frequently asked questions

Does a node replace the conversation history when it returns a message?
No. The context field uses the add_messages reducer, which appends new messages and skips any whose message_id is already present, so history is never overwritten.
How do I add my own fields to the state?
Subclass AgentState, add typed fields with defaults, and pass the class or an instance to StateGraph. The built-in context, context_summary and execution_meta fields stay available.
Can a tool change state as well as return text to the model?
Yes. Return a ToolResult with a message for the model and a state dict. Only the fields named in the dict are updated.
Last updated for v1.0.0Edit this page on GitHubReport an issue