Skip to main content

@llm_chat

@llm_chat creates a multi-turn conversational agent. It manages history, executes a ReAct loop with tools, streams responses, and optionally integrates with SelfRef for durable context.

Basic Usage

History Management

The history parameter (or chat_history) is special. The framework:
  1. Takes your provided history as the conversation transcript
  2. Appends the current user message
  3. Runs the ReAct loop
  4. Returns updated history in output.messages
History is external — you control storage, persistence, and branching.

Streaming

With stream=True, you receive chunks as events:
With stream=False, the model response arrives as a single ResponseYield.

Multimodal User Messages

For multimodal chat input, use exactly one canonical user-message object: UserChatMessage. This keeps @llm_chat as an Agent abstraction over one user turn and avoids multiple competing image-parameter styles.
UserChatMessage.multimodal(...) accepts text plus any number of ImgUrl / ImgPath values. It normalizes them to an OpenAI-compatible user message with text and image_url content parts. Future modalities should extend UserChatMessage, not introduce another chat input convention.

Tools

The ReAct loop handles tool calling automatically:
  1. LLM decides to call a tool → ToolCallStartEvent
  2. Framework executes the tool → ToolCallEndEvent
  3. The runtime records the tool result as an internal transcript patch
  4. Context is recompiled → LLM sees the patched transcript → decides next action
max_tool_calls limits total tool calls per invocation. Default is framework-defined. None means unlimited.

SelfRef Integration

For agents that need durable memory, context compaction, or sub-agent forking:
With self_reference_key, the framework:
  • Binds a SelfReference backend to this key
  • Creates a SelfRefSession per invocation
  • Makes selfref primitives available in PyRepl
  • Persists updated history after each turn
See SelfRef for the full context model.

Template Parameters

Inject runtime values into the system prompt:

Return Mode

System Prompt Construction

For @llm_chat, the final system prompt is built from multiple sources:
  1. Docstring → base system prompt (with template params applied)
  2. Tool best practices<tool_best_practices> block prepended
  3. Must principles<must_principles> block appended (use native tool calls)
  4. SelfRef experiences → rendered into the system prompt if active
  5. Latest system message in history → overrides docstring if present
Write your docstring as stable assistant policy — identity, behavioral rules, and long-lived constraints. Put per-turn context in arguments or template params.

Concurrent Sessions

Multiple independent conversations can run simultaneously:

Parameters Reference

Special call-time parameters:
  • _template_params: Dict[str, Any] — template values
  • _abort_signal: AbortSignal — cancellation
  • _too_long_to_file: bool — truncate long tool results to file
API Reference: Decorators