Agent Module¶
The core agent runtime with full-spectrum streaming.
BondAgent¶
Bases: Generic[T, DepsT]
Generic agent runtime wrapping PydanticAI with full-spectrum streaming.
A BondAgent provides: - High-fidelity streaming with callbacks for every lifecycle event - Block start/end notifications for UI rendering - Real-time streaming of text, thinking, and tool arguments - Tool execution and result callbacks - Message history management - Dynamic instruction override - Toolset composition - Retry handling
Example
agent = BondAgent(
name="assistant",
instructions="You are helpful.",
model="anthropic:claude-sonnet-4-20250514",
toolsets=[memory_toolset],
deps=QdrantMemoryStore(),
)
handlers = StreamHandlers(
on_text_delta=lambda t: print(t, end=""),
on_tool_execute=lambda id, name, args: print(f"[Running {name}]"),
)
response = await agent.ask("Remember my preference", handlers=handlers)
Methods:
-
ask–Send prompt and get response with high-fidelity streaming.
-
get_message_history–Get current conversation history.
-
set_message_history–Replace conversation history.
-
clear_history–Clear conversation history.
-
clone_with_history–Create new agent instance with given history (for branching).
ask(prompt, *, handlers=None, dynamic_instructions=None)
async
¶
Send prompt and get response with high-fidelity streaming.
Parameters:
-
prompt(str) –The user's message/question.
-
handlers(StreamHandlers | None, default:None) –Optional callbacks for streaming events.
-
dynamic_instructions(str | None, default:None) –Override system prompt for this call only.
Returns:
-
T–The agent's response of type T.
Source code in src/bond/agent.py
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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
get_message_history()
¶
set_message_history(history)
¶
clear_history()
¶
clone_with_history(history)
¶
Create new agent instance with given history (for branching).
Parameters:
-
history(list[ModelMessage]) –The message history to use for the clone.
Returns:
-
BondAgent[T, DepsT]–A new BondAgent with the same configuration but different history.
Source code in src/bond/agent.py
StreamHandlers¶
Callbacks mapping to every stage of the LLM lifecycle.
This allows the UI to perfectly reconstruct the Agent's thought process.
Lifecycle Events
on_block_start: A new block (Text, Thinking, or Tool Call) has started. on_block_end: A block has finished generating. on_complete: The entire response is finished.
Content Events (Typing Effect): on_text_delta: Incremental text content. on_thinking_delta: Incremental thinking/reasoning content. on_tool_call_delta: Incremental tool name and arguments as they form.
Execution Events
on_tool_execute: Tool call is fully formed and NOW executing. on_tool_result: Tool has finished and returned data.
Example
handlers = StreamHandlers(
on_block_start=lambda kind, idx: print(f"[Start {kind} #{idx}]"),
on_text_delta=lambda txt: print(txt, end=""),
on_tool_execute=lambda id, name, args: print(f"[Running {name}...]"),
on_tool_result=lambda id, name, res: print(f"[Result: {res}]"),
on_complete=lambda data: print(f"[Done: {data}]"),
)