Agent API¶
The core agent implementation for PatchPal, providing the main interface for interacting with LLMs and executing tools.
Creating an Agent¶
patchpal.agent.create_agent(model_id='anthropic/claude-sonnet-4-5', custom_tools=None, enabled_tools=None, litellm_kwargs=None)
¶
Create and return a PatchPal agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
LiteLLM model identifier (default: anthropic/claude-sonnet-4-5) |
'anthropic/claude-sonnet-4-5'
|
custom_tools
|
Optional[List[Callable]]
|
Optional list of Python functions to use as custom tools. Each function should have type hints and a docstring. |
None
|
enabled_tools
|
Optional[List[str]]
|
Optional list of tool names to enable (whitelist). If provided, only these built-in tools will be available. Custom tools are always added. Takes precedence over PATCHPAL_ENABLED_TOOLS environment variable. Example: ["read_file", "edit_file", "run_shell"] |
None
|
litellm_kwargs
|
Optional[Dict[str, Any]]
|
Optional dict of extra parameters to pass to litellm.completion() (e.g., {"reasoning_effort": "high"} for reasoning models) |
None
|
Returns:
| Type | Description |
|---|---|
PatchPalAgent
|
A configured PatchPalAgent instance |
Example
def calculator(x: int, y: int) -> str: '''Add two numbers.
Args:
x: First number
y: Second number
'''
return str(x + y)
agent = create_agent(custom_tools=[calculator]) response = agent.run("What's 5 + 3?")
Limit to read-only tools¶
agent = create_agent( enabled_tools=["read_file", "read_lines", "code_structure"] )
With reasoning model¶
agent = create_agent( model_id="ollama_chat/gpt-oss:120b", litellm_kwargs={"reasoning_effort": "high"} )
Source code in patchpal/agent/function_calling.py
patchpal.agent.create_react_agent(model_id='ollama_chat/llama3.2', custom_tools=None, enabled_tools=None, litellm_kwargs=None, custom_instructions='')
¶
Create and return a ReAct agent.
This agent uses text-based tool invocation instead of native function calling, making it compatible with models that don't support function calling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
LiteLLM model identifier (default: ollama_chat/llama3.2) |
'ollama_chat/llama3.2'
|
custom_tools
|
Optional[List[Callable]]
|
Optional list of Python functions to use as custom tools |
None
|
enabled_tools
|
Optional[List[str]]
|
Optional list of tool names to enable (whitelist) |
None
|
litellm_kwargs
|
Optional[Dict[str, Any]]
|
Optional dict of extra parameters for litellm.completion() |
None
|
custom_instructions
|
str
|
Optional custom instructions to prepend to system prompt |
''
|
Returns:
| Type | Description |
|---|---|
ReActAgent
|
A configured ReActAgent instance |
Example
Basic usage with Ollama¶
agent = create_react_agent(model_id="ollama_chat/llama3.2") response = agent.run("What files are in the src directory?")
With custom tools¶
def calculator(x: int, y: int) -> str: '''Add two numbers.''' return str(x + y)
agent = create_react_agent( model_id="ollama_chat/qwen2.5", custom_tools=[calculator] )
Source code in patchpal/agent/react.py
ReAct Agent for Local Models
For local models that don't support native function calling, use create_react_agent() instead of create_agent().
See Local Models - ReAct Mode for details.
Agent Classes¶
PatchPalAgent (Function Calling)¶
patchpal.agent.PatchPalAgent(model_id='anthropic/claude-sonnet-4-5', custom_tools=None, enabled_tools=None, litellm_kwargs=None)
¶
Simple agent that uses LiteLLM for tool calling.
Initialize the agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
LiteLLM model identifier |
'anthropic/claude-sonnet-4-5'
|
custom_tools
|
Optional[List[Callable]]
|
Optional list of Python functions to add as tools |
None
|
enabled_tools
|
Optional[List[str]]
|
Optional list of tool names to enable (whitelist). If provided, only these tools will be available. Takes precedence over PATCHPAL_ENABLED_TOOLS environment variable. |
None
|
litellm_kwargs
|
Optional[Dict[str, Any]]
|
Optional dict of extra parameters to pass to litellm.completion() (e.g., {"reasoning_effort": "high"} for reasoning models) |
None
|
Source code in patchpal/agent/function_calling.py
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 | |
run(user_message, max_iterations=100)
¶
Run the agent on a user message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_message
|
str
|
The user's request |
required |
max_iterations
|
int
|
Maximum number of agent iterations (default: 100) |
100
|
Returns:
| Type | Description |
|---|---|
str
|
The agent's final response |
Source code in patchpal/agent/function_calling.py
ReActAgent (Text-Based Tool Calling)¶
patchpal.agent.ReActAgent(model_id='ollama_chat/llama3.2', custom_tools=None, enabled_tools=None, litellm_kwargs=None, custom_instructions='')
¶
Agent that uses ReAct pattern instead of native function calling.
Initialize ReAct agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
LiteLLM model identifier |
'ollama_chat/llama3.2'
|
custom_tools
|
Optional[List[Callable]]
|
Optional list of custom Python functions to add as tools |
None
|
enabled_tools
|
Optional[List[str]]
|
Optional list of tool names to enable (whitelist) |
None
|
litellm_kwargs
|
Optional[Dict[str, Any]]
|
Optional dict of extra parameters for litellm.completion() |
None
|
custom_instructions
|
str
|
Optional custom instructions to prepend to system prompt |
''
|
Source code in patchpal/agent/react.py
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 | |
run(user_message, max_iterations=100)
¶
Run the agent on a user message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_message
|
str
|
The user's question/request |
required |
max_iterations
|
int
|
Maximum number of iterations before giving up |
100
|
Returns:
| Type | Description |
|---|---|
str
|
The agent's final answer |
Source code in patchpal/agent/react.py
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 | |
Usage Example¶
from patchpal.agent import create_agent
# Create agent with default model
agent = create_agent()
# Or specify a model
agent = create_agent(model_id="anthropic/claude-sonnet-4-5")
# Run a task
response = agent.run("List all Python files")
print(response)
# Check token usage
print(f"Total tokens: {agent.cumulative_input_tokens + agent.cumulative_output_tokens:,}")
Related¶
- Python API Guide - Comprehensive guide to using the Python API
- Context Management - How context windows are managed
- Custom Tools - Adding your own tools to the agent