An Agent wraps an LLM in a loop: it sends the conversation and its tools to the model, runs the tools the model asks for, sends the results back, and repeats until the model answers without calling a tool. The same object keeps its history between runs, so it carries a conversation.

Configuration

Context-management options (max_tool_output_tokens, warn_near_limits, on_context_usage) are described in Context management.

The system prompt

Unless you give a complete system_prompt, the agent builds one for every request from, in this order:
  1. ## Instructions: each entry of instructions as a bullet;
  2. ## Skills: the name and description of each skill;
  3. the JSON schema of the response_model;
  4. ## Memory: the long-term memory block;
  5. <references>: the passages the retriever found for the task;
  6. <context>: the context passed to the run.
The stable parts come first and the per-run parts last, so the prefix the provider caches stays identical from one request to the next. agent.get_system_prompt(task, context) returns what the model would receive. Instructions that take parameters can be rendered with PromptTemplate (gwenflow.prompts), a thin wrapper over str.format that can read a template from a file:

Running

The input is a string, a list of messages or a list of OpenAI-style dicts. run_stream and arun_stream yield events as the run progresses; see Streaming.

Passing context

context adds information to the system prompt for this run only, without storing it in the history:
A string is wrapped in a <context> block; a dict gives one block per key.

Conversations

The history persists on the agent, so successive runs form a conversation:
To serve several users, give each conversation its own agent: agent.clone() returns a copy with the same configuration and an empty history. To restore a saved conversation, add its messages to the history before running:

The response

run returns an AgentResponse: content, parsed, finish_reason (stop, max_turns, approval_required), messages, events, usage and delegation_usage. See Messages and responses.

Limits

max_turns caps the number of model calls in a run. When it is reached, the run ends with finish_reason="max_turns" and no answer:
With warn_near_limits=True the model is told, in the system prompt of the request, when it is close to the limit, so it concludes instead of being cut off. See Context management.

Usage

response.usage sums the tokens, requests and tool calls of the run, including the summaries of a compaction and the runs of delegated agents. agent.total_usage sums every run of the agent:
AgentUsage also counts cache_read_tokens and cache_write_tokens for providers that report prompt caching. The library does not price tokens itself; Agent(pricing={"input": 3.0, "output": 15.0}) (dollars per million tokens, with optional cache_read and cache_write) is a place to keep your grid for callers that estimate costs from total_usage.

Tools on the agent

Besides tools=[...], a function becomes a tool of one agent with the @agent.tool decorator:
See Tools and Tool approvals.

Retrieval

With a retriever, the agent searches it with the task before calling the model and adds the best passages to the system prompt as references:
See Retrieval (RAG).