Skip to main content

Building Custom Agents

While AutoGen provides built-in agents like AssistantAgent and CodeExecutorAgent, you can create custom agents with specialized behaviors by extending the BaseChatAgent class.

Understanding BaseChatAgent

All agents in AutoGen inherit from BaseChatAgent and must implement:
  • on_messages: Defines agent behavior in response to messages
  • on_reset: Resets the agent to its initial state
  • produced_message_types: List of message types the agent can produce
Optionally, implement on_messages_stream for streaming responses.

Simple Custom Agent Example

Here’s a countdown agent that produces a stream of messages:

Arithmetic Agent Example

A more practical example - an agent that performs arithmetic operations:
The on_messages method may be called with an empty list, meaning the agent was called previously without new messages. Always maintain message history internally.

Custom Agent with LLM

Create custom agents that use LLMs with custom behavior:

State Management

Implement state persistence for your custom agents:

Using Custom Agents in Teams

Custom agents work seamlessly with AutoGen’s team patterns:

Best Practices

Custom agents are not thread-safe or coroutine-safe. Do not share agents between multiple tasks or call methods concurrently.

Advanced: Nested Agents

Create agents that contain other agents or teams: