ConversableAgent: Building Intelligent Agents
The ConversableAgent
is the fundamental building block of AG2 - think of it as both the brain and the personality of your AI system. With an LLM configuration providing its thinking power, a ConversableAgent can:
- Communicate with other agents and humans
- Process information and generate responses
- Follow instructions defined in its system message
- Execute tools and functions when needed
Every agent in your AG2 system is either a ConversableAgent
or built upon one, making it the most important class to understand.
Creating a ConversableAgent
Once you’ve set up your LLM configuration, creating a ConversableAgent
is straightforward:
Key Parameters
When creating a ConversableAgent
, there are several important parameters to consider:
name
: A unique identifier for your agentsystem_message
: Instructions that define the agent’s role, personality, and behaviorllm_config
: Configuration for the language model (can be passed directly or via context manager)
Interacting with a ConversableAgent
The simplest way to interact with a ConversableAgent
is to use the run()
and process()
methods. Here’s a basic example:
???+ info
Why Two Steps: run()
and process()
You might wonder why we need to call both run()
and process()
to get results.
👉 Here’s what’s happening:
When you call run()
, it doesn’t immediately give you the final output.
Instead, it returns an iterator, a special object that holds a stream of events, messages, and metadata.
👉 Why? Because flexibility matters.
The workflow steps won’t actually start running until you iterate over this iterator. This design gives you full control and makes it easy to build things like:
- Custom UIs
- Real-time dashboards
- Interactive apps where you control how and when each event is handled
👉 Okay — so what does process()
do?
process()
is a built-in helper method that takes care of iterating through those events for you.
It simulates a chat-like console experience — printing messages, handling user inputs, and making it feel like a live conversation.
👉 In short:
Use run()
and iterate over the events yourself when you want full control over the workflow’s events and how they are processed. 👉 Learn more about the run()
method here →
Use process()
along with run()
when you just want a quick, ready-to-go chat experience in the console.
Financial Compliance Example
Let’s create a simple financial agent using ConversableAgent
:
In this simple example:
- We’ve created a financial agent with a basic system message
- We’re asking it a straightforward question about suspicious transactions
- We’re limiting the conversation to just one turn by setting
max_turns=1
. This is one of the many ways to end a conversation. You can find more options in the Ending a chat.
How to Run This Example
- Save the code above to a file (e.g.,
financial_compliance.py
) - Set your OpenAI API key in your environment variable or use your preferred model provider
- Make sure you have AG2 installed:
pip install ag2[openai]
- Run the script:
python financial_compliance.py
Example Output
When you run the code, you should see output similar to this:
This is just the beginning of what you can do with ConversableAgent
. In subsequent sections, we’ll expand this example to handle more complex financial tasks and interactions.
Next Steps
Now that you understand the basics of ConversableAgent
, it’s time to add some human oversight!
For financial compliance systems, a human review of flagged transactions adds an essential layer of security and compliance. In the next section, you’ll see how to build upon our simple example to create a more robust financial assistant with human oversight.