Overview
Agents are the core building blocks of the Qwen-Agent framework. An agent can receive messages and provide responses using LLMs, tools, or specialized workflows. Different agents have distinct processing patterns implemented in their_run method.
Agent Base Class
All agents inherit from theAgent base class, which provides the foundational interface for message processing and tool integration.
Key Components
Constructor Parameters
List of tools the agent can use. Can be:
- Tool name:
'code_interpreter' - Tool configuration:
{'name': 'code_interpreter', 'timeout': 10} - Tool object:
CodeInterpreter()
LLM configuration or model object. Example:
{'model': 'qwen-plus', 'api_key': 'your-key'}System prompt that guides the agent’s behavior
Agent name, useful for multi-agent scenarios
Agent description, used in multi-agent routing
Agent Workflow
Running an Agent
Agents provide two main interfaces for execution:Message Flow
Built-in Agent Types
BasicAgent
The simplest agent that only uses the LLM without tools or special workflows.qwen_agent/agent.py:263-269
FnCallAgent
A function-calling agent that can use tools through the LLM’s function calling capabilities. It automatically handles the tool invocation loop.- Automatically detects function calls from LLM responses
- Executes tools and feeds results back to the LLM
- Supports iterative tool use (tool → LLM → tool → …)
- Maximum iterations controlled by
MAX_LLM_CALL_PER_RUN
qwen_agent/agents/fncall_agent.py:27-121
Assistant
A comprehensive agent with RAG (Retrieval-Augmented Generation) capabilities and function calling. Ideal for document Q&A and general assistance tasks.- Built-in document retrieval and knowledge injection
- Supports multiple file formats (PDF, DOCX, TXT, etc.)
- Automatically formats retrieved content into context
- Inherits all FnCallAgent capabilities
qwen_agent/agents/assistant.py:81-149
Router
A multi-agent coordinator that routes requests to specialized sub-agents based on their capabilities.- Intelligently routes requests to appropriate sub-agents
- Falls back to direct response when no agent is needed
- Supports dynamic agent selection
- Maintains conversation context across agent switches
qwen_agent/agents/router.py:36-107
Custom Agent Implementation
Create custom agents by implementing the_run method:
Tool Integration
Agents can access tools through thefunction_map dictionary:
Best Practices
Choose the Right Agent
- Use BasicAgent for simple LLM chat
- Use FnCallAgent for tool-enabled workflows
- Use Assistant for RAG + tools
- Use Router for multi-agent coordination
Memory Management
- Agents with RAG capabilities include built-in Memory
- Memory automatically manages document parsing and retrieval
- Configure
rag_cfgto control retrieval behavior
Streaming
- Use
agent.run()for streaming responses - Use
agent.run_nonstream()when you need the complete result - Streaming provides better UX for long-running tasks
System Messages
- Always set clear system messages to guide behavior
- System messages are prepended automatically if not present
- Keep them concise and specific to the agent’s role
Related Resources
Tools
Learn about the tool system and how to create custom tools
Function Calling
Deep dive into function calling implementation
Memory
Understand the memory system for RAG
LLM Configuration
Configure LLM models and parameters