ReAct Agent
The ReActAgent is the most powerful agent type in the framework, implementing the Reasoning and Acting loop.
How it Works
The ReAct agent operates in a loop:
-
Context Construction: It gathers the conversation history and a special System Prompt that defines its operational rules (parallel execution, thinking first, etc.).
-
Tool Discovery: It looks at the list of
available_agents(tools) and converts them into function definitions for the LLM. -
Generation: It sends the context to the LLM.
-
Execution:
- If the LLM calls the Thinking Tool (parallel to others), the thought is recorded.
- If the LLM calls other tools, they are executed in parallel (up to a concurrency limit).
-
Recursion: The results are added back to the context, and the loop repeats until the LLM produces a final answer or the max iterations are reached.
Implementation Details
When you subclass ReActAgent, you typically only need to define:
- get_llm(): Which LLM to use.
- available_agents(): Which tools (other BaseAgents) this agent can use.
class MyOrchestrator(ReActAgent):
def get_llm(self, user_id: str):
return GeminiLLM()
def available_agents(self, user_id: str):
return [
SearchAgent(),
DatabaseAgent(),
# ...
]
By default, the ReActAgent will use the ThinkingAgent as the thinking tool. Its implementation is streightforward, and is necessary to improve the performance of the reasoning loop.
It is possible to override its implementation by subclassing it and providing a custom implementation of the get_thinking_agent() method.
For example:
class ResponsiveThinkingAgent(ThinkingAgent):
async def execute(
self, user_id: str, context: Context, input_args: ThinkingInput
) -> ThoughtResponse:
context.emit_feedback(input_args.reasoning)
return await super().execute(user_id, context, input_args)
And then use it in your ReActAgent:
class MyOrchestrator(ReActAgent):
def get_thinking_agent(self):
return ResponsiveThinkingAgent()
In this example, the ResponsiveThinkingAgent emits a feedback message to the user, showing the reasoning process in real-time.
API Reference
agentswarm.agents.ReActAgent
Bases: BaseAgent[InputType, OutputType]
Source code in src/agentswarm/agents/react_agent.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
available_agents(user_id)
abstractmethod
Returns the available agents to be used by the agent.
Source code in src/agentswarm/agents/react_agent.py
64 65 66 67 68 69 | |
gather_with_concurrency(n, *tasks)
async
Runs tasks with a concurrency limit of n.
Source code in src/agentswarm/agents/react_agent.py
135 136 137 138 139 140 141 142 143 144 145 | |
get_default_agents()
Returns the default agents for implementing the ReActAgent loop.
Source code in src/agentswarm/agents/react_agent.py
53 54 55 56 57 58 59 60 61 62 | |
get_llm(user_id)
abstractmethod
Returns the LLM to be used by the agent.
Source code in src/agentswarm/agents/react_agent.py
33 34 35 36 37 38 | |
get_thinking_agent()
Returns the thinking agent to be used by the agent.
Source code in src/agentswarm/agents/react_agent.py
47 48 49 50 51 | |
prompt(user_id)
abstractmethod
Returns the prompt to be used by the agent.
Source code in src/agentswarm/agents/react_agent.py
40 41 42 43 44 45 | |