The building blocks
A run, step by step
- System prompt. Built from the agent’s
instructions(or a fixedsystem_prompt), the list of its skills, its long-term memory, the JSON schema of aresponse_model, references from its retriever and thecontextpassed to the run. - History. The new message joins the agent’s history. Before every request the history is measured against the model’s context window and compacted when it nears the limit.
- Model call. The LLM client sends the history and the tool schemas, and returns a provider-neutral
ModelResponse. - Tools. Each tool call is executed and its result appended as a
toolmessage; the loop goes back to the model. Calls to delegated agents (team members, sub-agents) run in parallel. - Answer. When the model answers without calling a tool, the run ends with
finish_reason="stop". It also ends atmax_turns, or pauses when a tool needs a human’s approval.
Sync, async and streaming
Every operation has four forms, with the same arguments:
A stream yields events as they happen (text deltas, thinking, tool starts and ends) and ends with the complete response. See Streaming.
Lazy imports
import gwenflow stays light: the vendor SDKs (anthropic, google-genai, mistralai), the tools, the readers and the MCP client load on first use. from gwenflow import ChatAnthropic works as usual; the SDK is only imported at that moment. Avoid from gwenflow import *, which resolves every name and loses the saving.