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 completesystem_prompt, the agent builds one for every request from, in this order:
## Instructions: each entry ofinstructionsas a bullet;## Skills: the name and description of each skill;- the JSON schema of the
response_model; ## Memory: the long-term memory block;<references>: the passages the retriever found for the task;<context>: thecontextpassed to the run.
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
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:
<context> block; a dict gives one block per key.
Conversations
The history persists on the agent, so successive runs form a conversation: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:
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
Besidestools=[...], a function becomes a tool of one agent with the @agent.tool decorator:
Retrieval
With aretriever, the agent searches it with the task before calling the model and adds the best passages to the system prompt as references: