Core API Reference
Agent
lllm.core.agent.Agent
dataclass
Represents a single LLM agent with a specific role and capabilities.
An Agent owns the dialogs it creates. Each dialog is keyed by a user-chosen alias (e.g. 'planning', 'talk_with_coder') that makes the code self-documenting:
agent.open('planning', prompt_args={...})
agent.receive("What's the plan?")
response = agent.respond()
agent.open('execution', prompt_args={...})
agent.switch('execution')
...
For power-user / cross-agent scenarios, call(dialog) still accepts
a raw Dialog directly — but the recommended path is alias-based.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name or role of the agent (e.g., 'assistant', 'coder'). |
system_prompt |
Prompt
|
The system prompt defining the agent's persona. |
model |
str
|
The model identifier (e.g., 'gpt-4o'). |
llm_invoker |
BaseInvoker
|
The invoker instance for LLM calls. |
model_args |
Dict[str, Any]
|
Additional model arguments (temp, top_p, etc.). |
max_exception_retry |
int
|
Max retries for agent parsing/validation exceptions. |
max_interrupt_steps |
int
|
Max consecutive tool call interrupts. |
max_llm_recall |
int
|
Max retries for LLM API errors. |
Source code in lllm/core/agent.py
17 18 19 20 21 22 23 24 25 26 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 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
current_dialog
property
The currently active dialog.
dialogs
property
Read-only snapshot of all managed dialogs (alias → Dialog).
close(alias)
Remove a dialog from this agent and return it.
Useful for archiving, handing off to another system, or just cleaning up. If the closed dialog was active, active becomes None.
Source code in lllm/core/agent.py
fork(alias, child_alias, last_n=0, first_k=1, switch=True)
Branch an existing dialog into a new child dialog.
The parent dialog's fork() handles all lineage bookkeeping
(parent ↔ child links, split_point, ids). Agent just stores
the child under child_alias and switches to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
the source dialog to fork from. |
required |
child_alias
|
str
|
the alias for the new child dialog. |
required |
last_n
|
int
|
if >0, drop the last n messages from the copy. |
0
|
first_k
|
int
|
if >0, keep the first k messages from the copy. Only used when last_n is >0. |
1
|
switch
|
bool
|
if True, switch to the new child dialog after forking. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
KeyError
|
if |
Source code in lllm/core/agent.py
open(alias, prompt_args=None, session_name=None, switch=True)
Create a new dialog owned by this agent, keyed by alias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
the alias for the new dialog. |
required |
prompt_args
|
Optional[Dict[str, Any]]
|
the arguments for the system prompt. |
None
|
session_name
|
Optional[str]
|
the name of the session for logging and checkpointing. |
None
|
switch
|
bool
|
if True, switch to the new dialog after opening. Default is True. |
True
|
Source code in lllm/core/agent.py
receive(text, alias=None, role=Roles.USER, name='user')
Put a text message into the active (or specified) dialog.
Source code in lllm/core/agent.py
receive_image(image, caption=None, alias=None, role=Roles.USER, name='user')
Put an image message into the dialog.
Source code in lllm/core/agent.py
receive_prompt(prompt, prompt_args=None, alias=None, role=Roles.USER, name='user')
Put a structured prompt message into the dialog.
Source code in lllm/core/agent.py
respond(alias=None, metadata=None, args=None, parser_args=None, return_session=False)
High-level: run the agent call loop on a dialog, return the response.
This is the recommended way to get a response. For full diagnostics
(call_state with retry info, model_args, etc.), use call() directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
the alias of the dialog to respond to. |
None
|
metadata
|
Optional[Dict[str, Any]]
|
additional metadata for the call. |
None
|
args
|
Optional[Dict[str, Any]]
|
additional arguments for the prompt. |
None
|
parser_args
|
Optional[Dict[str, Any]]
|
arguments for the output parser. |
None
|
return_session
|
bool
|
if True, return the entire AgentCallSession instead of just the message (use session.delivery to get the final message). |
False
|
Source code in lllm/core/agent.py
switch(alias)
Set the active dialog by alias. Returns self for chaining.
Raises:
| Type | Description |
|---|---|
KeyError
|
if |
Source code in lllm/core/agent.py
Tactic
lllm.core.tactic.Tactic
Bases: ABC
A Tactic is a local, functional unit of agentic behavior.
It defines HOW a group of agents solve a task — the "program" that wires callers (agents) to functions (prompts).
Config format (the dict passed to __init__)::
tactic_type: analytica
global:
model_name: gpt-4o
model_args:
temperature: 0.1
agent_configs:
- name: analyzer
system_prompt_path: analytica/analyzer_system
model_args:
max_completion_tokens: 20000
- name: synthesizer
system_prompt_path: analytica/synthesizer_system
global provides defaults merged into each agent config.
agent_configs is a list; each entry must have a name.
Source code in lllm/core/tactic.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
quick(query=None, system_prompt='You are a helpful assistant.', model='gpt-4o', return_agent=False, **model_args)
classmethod
Quick constructor for a single-agent chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Optional[str]
|
The query to send to the agent. |
None
|
system_prompt
|
Optional[Union[str, Prompt]]
|
The system prompt to use for the agent. |
'You are a helpful assistant.'
|
model
|
str
|
The model to use for the agent. |
'gpt-4o'
|
return_agent
|
bool
|
If True, return the agent in addition to the response. If no query is provided, return the agent. |
False
|
**model_args
|
Any
|
Additional model arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Message, Agent, Tuple[Message, Agent]]
|
If return_agent is False: Message: The response from the agent. |
Union[Message, Agent, Tuple[Message, Agent]]
|
If return_agent is True: Tuple[Message, Agent]: The response from the agent and the agent. |
Union[Message, Agent, Tuple[Message, Agent]]
|
If return_agent is True and query is not provided: Agent: The agent. |
Source code in lllm/core/tactic.py
Dialog
lllm.core.dialog.Dialog
dataclass
An append-only message sequence owned by a single agent.
The agent that creates a dialog seeds it with its system prompt, and that
ownership is recorded on the tree_node. Other participants (user,
tools, forwarded messages from other agents) append via put_* helpers,
but the system-level identity of the dialog never changes.
Tree structure is maintained by a :class:DialogTreeNode owned by
each Dialog. fork() creates a child Dialog whose tree_node is
automatically linked to the parent's tree_node — callers (including
Agent) never need to wire lineage manually.
Source code in lllm/core/dialog.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
children
property
Live references to child Dialogs forked from this one.
parent
property
Live reference to parent Dialog (None for root dialogs).
fork(last_n=0, first_k=1)
Create a child dialog branching from this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_n
|
int
|
if >0, only preserve the last n messages from the parent dialog (useful for retrying from an earlier point). The first system message is always preserved. |
0
|
first_k
|
int
|
if >0, ensure the first k messages from the parent dialog (useful for preserving the system prompt message). Should always be >=1 to at least preserve the system prompt message. |
1
|
The fork automatically: - Deep-copies the message prefix into the child. - Creates a child DialogTreeNode linked to this dialog's tree_node. - Records split_point on the child's tree_node. - Wires live Dialog-level parent/children refs. - Inherits session_name, top_prompt, runtime, owner.
Returns:
| Type | Description |
|---|---|
'Dialog'
|
The new child Dialog. |
Source code in lllm/core/dialog.py
put_image(image, caption=None, name='user', metadata=None, role=Roles.USER)
Expects: - image: a base64 encoded string, a Path object or string path, or a PIL Image object
Source code in lllm/core/dialog.py
tree_overview(indent=0)
Recursively print the dialog tree structure from this node. Useful for debugging multi-fork scenarios.
Source code in lllm/core/dialog.py
Message
lllm.core.dialog.Message
Bases: BaseModel
Source code in lllm/core/dialog.py
Prompt
lllm.core.prompt.Prompt
Bases: BaseModel
A Prompt is a complete behaviour definition for one agent turn.
It bundles four concerns:
- Template — the text to send to the LLM, with
{variable}placeholders rendered viastr.format(or a customrenderer). - Output contract — an :class:
OutputSpecdescribing how to parse and validate the LLM's response. - Tools — the :class:
Functionand :class:MCPobjects available during this turn. - Handlers — template strings (or full Prompts) that define how to recover from exceptions and how to feed tool results back.
Provider-specific features (web search, computer use, citations, …) live
in the generic capabilities dict so that new features never require
schema changes on Prompt.
Notes
The __call__ method uses str.format by default, so literal braces
in the template must be doubled: {{ and }}.
Source code in lllm/core/prompt.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 | |
template_vars
property
Variable names required by this template (e.g. {topic} → 'topic').
__call__(**kwargs)
Render the template with the given variables.
Source code in lllm/core/prompt.py
extend(**overrides)
Create a new Prompt inheriting all fields, with overrides applied.
A new path is required — prompts that share a path would collide
in the registry::
child = parent.extend(
path="child/analysis",
prompt="More specific: {task}",
output=OutputSpec(parser=strict_parser),
)
Source code in lllm/core/prompt.py
get_function(name)
Retrieve a declared Function by name, with a clear error.
Source code in lllm/core/prompt.py
info_dict()
Return a JSON-serializable snapshot suitable for experiment tracking.
Source code in lllm/core/prompt.py
link_function(name, fn)
Attach a Python callable to an already-declared Function by name.
Source code in lllm/core/prompt.py
validate_args(prompt_args)
Return list of missing required template variables for the prompt.
Function
lllm.core.prompt.Function
Bases: BaseModel
Declarative description of a callable tool.
The schema (name, description, properties, required) describes the tool
to the LLM. The implementation is attached separately via
:meth:link_function or by using the :func:tool decorator which does
both in one step.
Source code in lllm/core/prompt.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
from_callable(fn, *, name=None, description=None, prop_desc=None, strict=True, processor=_default_function_call_processor)
classmethod
Build a :class:Function by inspecting fn's signature and
docstring. Type hints are converted to JSON Schema types.
Parameters without a type annotation default to "string".
Parameters whose names end with * in the docstring (or that
lack defaults) are treated as required.
For example:
@tool(
description="Get the current weather in a given location"
prop_desc={
"location": "The city and state, e.g. San Francisco, CA",
"unit": "The unit of temperature, e.g. celsius, fahrenheit",
}
)
def get_weather(location: str, unit: str = "celsius") -> str:
... # whatever you want to return, be sure to return a string at the end
Source code in lllm/core/prompt.py
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
link_function(fn)
Attach the Python callable that backs this tool.
FunctionCall
lllm.core.const.FunctionCall
Bases: BaseModel
One invocation of a tool, including its result once executed.