Agent Module

Agent orchestration utilities for LLM applications.

This module provides comprehensive agent frameworks and execution patterns.

Import Structure:

## Top-level imports (most common): `python from kerb.agent import Agent, run_agent, AgentResult from kerb.agent import Tool, create_tool `

## Submodule imports (for organized access): `python from kerb.agent import patterns, tools, reasoning, memory, planning, teams, monitoring, execution # Then: patterns.ReActAgent, tools.ToolRegistry, reasoning.Chain, execution.run_agent_loop `

## Direct submodule access: `python from kerb.agent.patterns import ReActAgent, PlanAndExecuteAgent from kerb.agent.tools import validate_tool_call, global_tool_registry from kerb.agent.reasoning import Chain, SequentialChain, ParallelChain from kerb.agent.memory import AgentMemory, WorkingMemory from kerb.agent.planning import Planner, create_plan from kerb.agent.teams import AgentTeam, delegate_task from kerb.agent.monitoring import AgentTracer, evaluate_agent from kerb.agent.execution import run_agent_loop, run_agent_async `

Key Components:

  • Core: Agent, AgentResult, AgentStep, AgentState, AgentStatus

  • Execution: run_agent, run_agent_loop, run_agent_async, run_agent_stream, AgentExecutor

  • Patterns: ReActAgent, PlanAndExecuteAgent, ChainOfThoughtAgent, TreeOfThoughtAgent, ReflexAgent

  • Tools: Tool, ToolRegistry, create_tool, register_tool, execute_tool, global_tool_registry

  • Reasoning: Chain, SequentialChain, ParallelChain, ConditionalChain, LoopChain

  • Memory: AgentMemory, WorkingMemory, EpisodicMemory, save_agent_state, load_agent_state

  • Planning: Planner, Plan, create_plan, execute_plan, replan, validate_plan

  • Teams: AgentTeam, Conversation, delegate_task, aggregate_results

  • Monitoring: AgentTracer, evaluate_agent, benchmark_agent, compare_agents

class kerb.agent.Agent(name='Agent', llm_func=None, tools=None, max_iterations=10, memory=None, verbose=False)[source]

Bases: ABC

Base class for all agents.

An agent perceives its environment, reasons about it, and takes actions to achieve its goals.

__init__(name='Agent', llm_func=None, tools=None, max_iterations=10, memory=None, verbose=False)[source]

Initialize agent.

Parameters:
  • name (str) – Name of the agent

  • llm_func (Optional[Callable]) – Function to call LLM (should accept prompt and return response)

  • tools (Optional[List[Any]]) – List of tools available to the agent

  • max_iterations (int) – Maximum number of iterations

  • memory (Optional[AgentMemory]) – Agent memory instance

  • verbose (bool) – Whether to print execution details

abstractmethod run(goal, context=None)[source]

Run the agent to achieve a goal.

Parameters:
  • goal (str) – The goal to achieve

  • context (Dict[str, Any]) – Additional context for execution

Return type:

AgentResult

Returns:

AgentResult with output and execution steps

kerb.agent.run_agent(agent, goal, context=None)[source]

Run agent to achieve goal.

Parameters:
  • agent (Agent) – Agent instance to run

  • goal (str) – Goal to achieve

  • context (Dict[str, Any]) – Additional context

Return type:

AgentResult

Returns:

AgentResult with output and steps

class kerb.agent.AgentResult(output, steps, status, total_time, metadata=<factory>)[source]

Bases: object

Result from agent execution.

output

Final output from the agent

steps

List of execution steps

status

Final status of execution

total_time

Total execution time in seconds

metadata

Additional result metadata

output: str
steps: List[AgentStep]
status: AgentStatus
total_time: float
metadata: Dict[str, Any]
to_dict()[source]

Convert result to dictionary.

Return type:

Dict[str, Any]

__init__(output, steps, status, total_time, metadata=<factory>)
class kerb.agent.Tool(name, description, func, parameters=<factory>, returns='', examples=<factory>)[source]

Bases: object

Represents a tool/function that an agent can use.

name

Tool name

description

What the tool does

func

The actual function to execute

parameters

Parameter descriptions

returns

Return value description

examples

Usage examples

name: str
description: str
func: Callable
parameters: Dict[str, Dict[str, Any]]
returns: str = ''
examples: List[str]
execute(*args, **kwargs)[source]

Execute the tool.

Parameters:
  • *args – Positional arguments

  • **kwargs – Keyword arguments

Return type:

ToolResult

Returns:

ToolResult with output or error

async execute_async(*args, **kwargs)[source]

Execute tool asynchronously.

Parameters:
  • *args – Positional arguments

  • **kwargs – Keyword arguments

Return type:

ToolResult

Returns:

ToolResult

to_dict()[source]

Convert tool to dictionary representation.

Return type:

Dict[str, Any]

to_openai_function()[source]

Convert to OpenAI function calling format.

Return type:

Dict[str, Any]

Returns:

Dictionary in OpenAI function format

to_anthropic_tool()[source]

Convert to Anthropic tool format.

Return type:

Dict[str, Any]

Returns:

Dictionary in Anthropic tool format

__str__()[source]

String representation.

Return type:

str

__repr__()[source]

Repr.

Return type:

str

__init__(name, description, func, parameters=<factory>, returns='', examples=<factory>)
kerb.agent.create_tool(name, func, description='', parameters=None, returns='', examples=None, auto_parse=True)[source]

Create a tool from a function.

Parameters:
  • name (str) – Tool name

  • func (Callable) – Function to wrap

  • description (str) – Tool description

  • parameters (Dict[str, Dict[str, Any]]) – Parameter specifications

  • returns (str) – Return value description

  • examples (List[str]) – Usage examples

  • auto_parse (bool) – Auto-parse parameters from function signature

Return type:

Tool

Returns:

Tool instance

Agent orchestration and execution patterns for multi-step reasoning.