Agent
Base classes for agents and nodes.
Classes:
-
Agent
–Base class for agents within the TapeAgents framework.
-
AgentStream
–A wrapper around a generator that produces AgentEvents, representing the result of an agent run.
-
Annotator
–Annotator is the base class for agents that produce annotations for the tape of another agent.
-
Node
–A node in the agent, atomic unit of the agent's behavior.
-
TapeReuseFailure
–Exception raised when tape reuse operation fails.
Agent
Bases: BaseModel
, Generic[TapeType]
Base class for agents within the TapeAgents framework.
An agent is a model that can run on a tape, generating new steps based on the tape's state. The agent can have subagents, which are other agents that it manages and can delegate to. The agent can also have nodes, which are atomic units of the agent's behavior that it can choose to run based on the tape.
Attributes:
-
name
(str
) –The unique name of the agent.
-
llms
(dict[str, SerializeAsAny[LLM]]
) –A dictionary mapping names to LLM instances used by the agent.
-
subagents
(list[Any]
) –A list of subagents managed by this agent. Subagents must have unique names.
-
templates
(dict[str, Any]
) –A dictionary of templates used for generating prompts.
-
nodes
(list[SerializeAsAny[Node]]
) –A list of nodes that define the agent's actions and decision points.
-
max_iterations
(int
) –The maximum number of iterations the agent will execute before stopping.
-
manager
(Agent
) –Retrieves the manager agent overseeing this agent.
-
llm
(LLM
) –Default language model if only one is configured.
-
template
(Template
) –Default template if only one is configured.
-
full_name
(str
) –Hierarchical name of the agent, including its manager hierarchy.
Raises:
-
ValueError
–If configuration inconsistencies are detected:
- If a subagent is already managed by another agent
- If any subagent is not an instance of Agent class
- If there are duplicate names among subagents
- If there are duplicate names among nodes
Methods:
-
clone
–Creates a deep copy of the current agent instance.
-
compute_view
–Compute the view stack from a given tape.
-
create
–Creates an instance of the class with provided LLMs and templates.
-
delegate
–Delegates control to the appropriate subagent based on the current tape state.
-
find_node
–Find a node by its name in the list of nodes.
-
find_subagent
–Find a subagent by name in the list of subagents.
-
generate_steps
–Generate steps from the agent by selecting a node and processing its output.
-
get_node_runs
–Parse the tape and identify the indices where each node began its execution.
-
get_subagent_names
–Returns a list of names of all subagents.
-
is_agent_step
–Check if a step was produced by the agent.
-
make_llm_output
–Generates an LLM output based on a tape and step index.
-
make_prompt
–Makes the prompt for the next iteration of the agent.
-
make_training_data
–Generates training data from a tape by converting LLM calls into training texts.
-
make_training_text
–Routes the request to make training text to the agent's LLM.
-
reuse
–Reuse another agent's tape as one's own.
-
run
–Run the agent on the tape iteratively, delegating to subagents until a stop condition is met.
-
run_iteration
–Run one iteration of the agent (assuming one call to the underlyng model).
-
select_node
–Select the next node to execute based on the current state of the tape.
-
should_stop
–Check if the agent should stop its turn and wait for observations.
-
update
–Updates the agent's configuration while preserving instance types.
Attributes:
-
full_name
–Returns the full hierarchical name of the agent.
-
llm
–Get the default LLM instance associated with the agent.
-
manager
–Gets the manager of the agent.
-
template
–Returns the default template of the agent.
Source code in tapeagents/agent.py
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 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 587 588 589 590 591 592 593 594 595 596 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 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
|
full_name
property
Returns the full hierarchical name of the agent.
The full name is constructed by combining the manager's full name (if present) with this agent's name, separated by a forward slash. If the agent has no manager, returns just the agent's name.
Returns:
-
str
–The full hierarchical name path of the agent. Examples: "agent_name" (no manager), "manager_name/agent_name" (with manager)
llm
property
Get the default LLM instance associated with the agent.
Returns:
-
LLM
–The default LLM instance if only one LLM is configured.
Raises:
-
ValueError
–If multiple LLMs are configured for this agent. In this case, use the
llms
property to access specific LLM instances.
manager
property
Gets the manager of the agent.
Returns:
-
Agent
–The manager agent instance.
Raises:
-
ValueError
–If the agent doesn't have a manager assigned.
template
property
Returns the default template of the agent.
This property provides access to the default template when the agent has exactly one template. If multiple templates exist, it raises a ValueError indicating that specific templates should be accessed through the templates property instead.
Returns:
-
Template
–The default template object.
Raises:
-
ValueError
–If the agent has more than one template.
-
IndexError
–If no templates exist (implicitly through list access).
clone()
Creates a deep copy of the current agent instance.
This method creates an independent copy of the agent with all its attributes, but detaches it from any manager.
Returns:
-
Self
(Self
) –A new instance of the agent with identical attributes but no manager.
Source code in tapeagents/agent.py
349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
compute_view(tape)
Compute the view stack from a given tape.
Parameters:
-
tape
(TapeType
) –The input tape to process.
Returns:
-
TapeViewStack
(TapeViewStack
) –A stack of views computed from the input tape.
Source code in tapeagents/agent.py
450 451 452 453 454 455 456 457 458 459 460 |
|
create(llms=None, templates=None, **kwargs)
classmethod
Creates an instance of the class with provided LLMs and templates.
Parameters:
-
llms
(Union[Dict[str, LLM], LLM, None]
, default:None
) –Language model(s) to use. Can be:
- A dictionary mapping names to LLM instances
- A single LLM instance (will be mapped to default name)
- None (empty dict will be used)
-
templates
(Union[Dict[str, Any], str, None]
, default:None
) –Template(s) to use. Can be:
- A dictionary mapping names to template configurations
- A single template string (will be mapped to default name)
- None (no templates will be used)
-
**kwargs
(dict
, default:{}
) –Additional keyword arguments to pass to the class constructor
Returns:
-
Self
(Self
) –A new instance of the class initialized with the provided arguments
Example
agent = Agent.create(llm) # Single LLM
agent = Agent.create({"gpt": llm1, "claude": llm2}) # Multiple LLMs
agent = Agent.create(llm, "template") # LLM with template
Source code in tapeagents/agent.py
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 |
|
delegate(tape)
Delegates control to the appropriate subagent based on the current tape state.
This method recursively traverses the agent hierarchy to find the most specific subagent that should handle the current tape state based on views computed from the tape.
Parameters:
-
tape
(TapeType
) –The tape containing the current state to process.
Returns:
Source code in tapeagents/agent.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
|
find_node(name)
Find a node by its name in the list of nodes.
Parameters:
-
name
(str
) –The name of the node to find.
Returns:
-
Node
–The node with the matching name.
Raises:
-
ValueError
–If no node with the given name is found.
Source code in tapeagents/agent.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
find_subagent(name)
Find a subagent by name in the list of subagents.
Parameters:
-
name
(str
) –The name of the subagent to find.
Returns:
-
Agent
–The found subagent instance.
Raises:
-
ValueError
–If no subagent with the given name is found.
Source code in tapeagents/agent.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
generate_steps(tape, llm_stream)
Generate steps from the agent by selecting a node and processing its output.
Parameters:
-
tape
(TapeType
) –The input tape containing the interaction history
-
llm_stream
(LLMStream
) –Stream interface for the language model output
Yields:
-
Step | PartialStep
–Union[Step, PartialStep]: Union[Step, PartialStep]: The generated steps or partial steps.
Source code in tapeagents/agent.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
|
get_node_runs(tape)
Parse the tape and identify the indices where each node began its execution.
This method identifies transition points in the tape where different nodes started producing output by tracking changes in prompt IDs.
Parameters:
-
tape
(TapeType
) –The sequence of tape steps to analyze.
Returns:
-
list[tuple[Node, int]]
–list[tuple[Node, int]]: List of tuples containing (node, index) pairs where:
- node: The Node object that produced the tape fragment
- index: The starting index in the tape where this node began execution
Source code in tapeagents/agent.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
get_subagent_names()
Returns a list of names of all subagents.
Returns:
-
list[str]
–list[str]: A list containing the names of all subagents in the agent.
Source code in tapeagents/agent.py
340 341 342 343 344 345 346 347 |
|
is_agent_step(step)
Check if a step was produced by the agent.
Parameters:
-
step
(Step
) –The step object to check.
Returns:
-
bool
(bool
) –True if the step is an Action or Thought (agent-produced), False otherwise.
Source code in tapeagents/agent.py
577 578 579 580 581 582 583 584 585 586 587 588 |
|
make_llm_output(tape, index)
Generates an LLM output based on a tape and step index.
Parameters:
-
tape
(TapeType
) –The input tape
-
index
(int
) –The position in the tape up to which to process.
Returns:
-
LLMOutput
(LLMOutput
) –The generated language model output for the tape segment.
Note
This method delegates the actual output generation to the selected node's make_llm_output method after selecting the appropriate node based on the tape segment up to the given index.
Source code in tapeagents/agent.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
make_prompt(tape)
Makes the prompt for the next iteration of the agent. This method generates a prompt by delegating to the selected node's make_prompt method. Can return a prompt with no messages, indicating the agent should generate next steps by following rules without LLM assistance. Agents that only delegate to subagents may not need to implement this method.
Parameters:
-
tape
(TapeType
) –The tape containing the agent's state and history
Returns:
-
Prompt
(Prompt
) –A prompt object for the next agent iteration, potentially empty
Note
- Empty prompts signal rule-based generation without LLM
- Method may be optional for pure delegation agents
Source code in tapeagents/agent.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
make_training_data(tape)
Generates training data from a tape by converting LLM calls into training texts.
Parameters:
-
tape
(TapeType
) –A tape containing recorded LLM interactions.
Returns:
-
list[TrainingText]
–list[TrainingText]: A list of training text objects created from the LLM calls.
Notes
This method first reuses the tape to extract LLM calls, then converts each call into a training text format using make_training_text().
Source code in tapeagents/agent.py
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
|
make_training_text(llm_call)
Routes the request to make training text to the agent's LLM.
Parameters:
-
llm_call
(LLMCall
) –Object containing prompt and output from an LLM call.
Returns:
-
TrainingText
(TrainingText
) –The training text generated from the prompt and output.
Note
Currently only supports one LLM. Future versions will support multiple LLMs.
Source code in tapeagents/agent.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
|
reuse(tape)
Reuse another agent's tape as one's own.
Construct LLM outputs at each step where a prompt is made. Check that output parsing yield the same steps as in the original tape. Rewrite metadata for all steps.
Parameters:
-
tape
(TapeType
) –The tape to reuse
Returns:
-
tuple[TapeType, list[LLMCall]]
–tuple[TapeType, list[LLMCall]]: The reused tape and a list of LLM calls made during the reuse
Raises:
-
TapeReuseFailure
–If the regenerated steps don't match the original tape.
Source code in tapeagents/agent.py
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 |
|
run(tape, max_iterations=None)
Run the agent on the tape iteratively, delegating to subagents until a stop condition is met.
This method executes the agent's logic by: 1. Delegating to appropriate subagents based on the tape state 2. Processing steps from subagent iterations 3. Updating the tape with new steps 4. Checking stop conditions 5. Tracking metadata about the execution
Parameters:
-
tape
(TapeType
) –The input tape to process
-
max_iterations
(int
, default:None
) –Maximum number of iterations to run. If None, uses self.max_iterations. Defaults to None.
Returns:
-
AgentStream[TapeType]
–AgentStream[TapeType]: A stream of AgentEvents containing:
- partial_step: Intermediate processing steps
- step: Completed agent steps with updated tape
- final_tape: Final tape with updated metadata after completion
Yields:
-
AgentEvent
(AgentStream[TapeType]
) –Events indicating the agent's progress including partial steps, completed steps with updated tape, and the final result.
Raises:
-
ValueError
–If the agent generates anything other than steps or partial steps.
Source code in tapeagents/agent.py
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 |
|
run_iteration(tape, llm_stream=None)
Run one iteration of the agent (assuming one call to the underlyng model).
During an iteration the agent generates steps from a stream of tokens that arises
from a single LLM call with a single prompt. An agent can do multiple iterations
before returning the next action (see run
method).
This function can also take a given llm_stream
, which can be useful when the agent
reuses a tape.
Parameters:
-
tape
(TapeType
) –The tape to run the agent on
-
llm_stream
(LLMStream
, default:None
) –The stream of tokens from the LLM
Yields:
-
Step | PartialStep
–Union[Step, PartialStep]: The generated steps or partial
Raises:
-
NotImplementedError
–If the agent has multiple LLMs and no LLM stream is provided
Source code in tapeagents/agent.py
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 |
|
select_node(tape)
Select the next node to execute based on the current state of the tape.
The selection process follows these rules
- If next_node is explicitly set in the tape view, return that node
- If no nodes have been run yet (last_node is None), return the first node
- Return the node that follows the last executed node in the list
Parameters:
-
tape
(TapeType
) –The tape containing execution state and data
Returns:
-
Node
(Node
) –The next node to be executed
Raises:
-
ValueError
–If unable to determine the next node to execute (e.g., reached end of list)
Source code in tapeagents/agent.py
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 |
|
should_stop(tape)
Check if the agent should stop its turn and wait for observations.
Parameters:
-
tape
(TapeType
) –The tape containing the sequence of steps (actions and observations).
Returns:
-
bool
(bool
) –True if the last step in the tape is an Action, indicating the agent should stop and wait for observations. False if the last step is not an Action, indicating the agent can continue.
Source code in tapeagents/agent.py
590 591 592 593 594 595 596 597 598 599 600 601 |
|
update(agent_config)
Updates the agent's configuration while preserving instance types.
This method allows reconfiguration of the agent while maintaining the class types of LLMs and subagents. It performs a deep update by recursively applying changes to nested components.
Parameters:
-
agent_config
(dict[str, Any]
) –New configuration dictionary containing LLMs, subagents, templates and other agent settings.
Returns:
Raises:
-
ValueError
–If the new configuration has different LLMs or number of subagents than the current agent.
Note
- Only string templates are updated, complex template objects are preserved
- Node configurations are preserved to avoid potential issues
Source code in tapeagents/agent.py
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 |
|
AgentStream
Bases: Generic[TapeType]
A wrapper around a generator that produces AgentEvents, representing the result of an agent run.
The generator can be iterated over to get the events, or the final tape can be extracted with get_final_tape. Support iterable protocol and generator protocol.
Attributes:
-
generator
(Generator[AgentEvent[TapeType], None, None]
) –The generator that produces AgentEvents.
Methods:
-
get_final_tape
–Retrieve the final tape from the agent's events.
-
get_steps
–Generator function that yields steps from events.
Source code in tapeagents/agent.py
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 |
|
get_final_tape()
Retrieve the final tape from the agent's events.
Iterates through the events of the agent and returns the final tape if it is found. If no final tape is produced by the agent, a ValueError is raised.
Returns:
-
TapeType
(TapeType
) –The final tape produced by the agent.
Raises:
-
ValueError
–If the agent did not produce a final tape.
Source code in tapeagents/agent.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
get_steps()
Generator function that yields steps from events.
Yields:
-
Step
(Step
) –The step associated with each event that has a step.
Source code in tapeagents/agent.py
83 84 85 86 87 88 89 90 91 92 |
|
Annotator
Bases: Agent[AnnotatorTapeType]
, Generic[TapeType, AnnotatorTapeType]
Annotator is the base class for agents that produce annotations for the tape of another agent. It annotates the tape by converting it into its own tape and then producing an annotation step appended to the converted tape.
Source code in tapeagents/agent.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
Node
Bases: BaseModel
A node in the agent, atomic unit of the agent's behavior.
The agent chooses which node to run based on the current tape. The node has a name and contains 2 main functions:
- make a prompt out of the tape
- generate steps out of the received llm output
Attributes:
-
name
(str
) –The name of the node. Defaults to an empty string.
Methods:
-
generate_steps
–Generates steps for the given agent, tape, and LLM stream.
-
make_llm_output
–Generates an LLMOutput object for a given agent and tape at a specified index.
-
make_prompt
–Creates a prompt for the given agent and tape.
Source code in tapeagents/agent.py
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 |
|
generate_steps(agent, tape, llm_stream)
Generates steps for the given agent, tape, and LLM stream.
Parameters:
-
agent
(Any
) –The agent for which steps are to be generated.
-
tape
(Tape
) –The tape object containing relevant data.
-
llm_stream
(LLMStream
) –The LLM stream to be used for generating steps.
Yields:
-
Step | PartialStep
–Union[Step, PartialStep]: The generated steps or partial steps.
Raises:
-
NotImplementedError
–If the method is not implemented by the subclass.
Source code in tapeagents/agent.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
make_llm_output(agent, tape, index)
Generates an LLMOutput object for a given agent and tape at a specified index.
Parameters:
-
agent
(Any
) –The agent for which the LLMOutput is being generated.
-
tape
(Tape
) –The tape containing the steps.
-
index
(int
) –The index of the step in the tape from which to generate the output.
Returns:
-
LLMOutput
(LLMOutput
) –An object containing the role and content for the LLM output.
Source code in tapeagents/agent.py
147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
make_prompt(agent, tape)
Creates a prompt for the given agent and tape.
Parameters:
-
agent
(Any
) –The agent for which the prompt is being created.
-
tape
(Tape
) –The tape associated with the agent.
Returns:
-
Prompt
(Prompt
) –The generated prompt.
Source code in tapeagents/agent.py
115 116 117 118 119 120 121 122 123 124 125 126 |
|
TapeReuseFailure
Bases: ValueError
Exception raised when tape reuse operation fails.
This exception is raised when an attempt to reuse a tape encounters an error, providing access to the succesfully reused part of the tape
Parameters:
-
msg
(str
) –Description of why the tape reuse failed
-
partial_tape
(Tape
) –The incomplete/partial tape that was being constructed
Attributes:
-
partial_tape
(Tape
) –The incomplete tape at the point of failure
Source code in tapeagents/agent.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
|