Agents¶
AgentHelm provides DSPy-native agents for tool calling and planning.
Quick Start¶
import dspy
from agenthelm import ToolAgent, PlannerAgent
# Configure your LLM
lm = dspy.LM("mistral/mistral-large-latest")
# Create a tool agent
agent = ToolAgent(
name="assistant",
lm=lm,
tools=[my_search_tool, my_summarize_tool],
max_iters=10
)
result = agent.run("What is the weather in NYC?")
print(result.answer)
# Create a planner agent
planner = PlannerAgent(
name="planner",
lm=lm,
tools=[search, summarize, write_file]
)
plan = planner.plan("Research AI trends and create a report")
print(plan.to_yaml()) # Review before execution
Agent Types¶
ToolAgent¶
ReAct-style agent that reasons and executes tools in a loop.
from agenthelm import ToolAgent, tool
@tool()
def get_weather(city: str) -> str:
"""Get weather for a city."""
return f"Sunny in {city}"
agent = ToolAgent(name="weather_bot", lm=lm, tools=[get_weather])
result = agent.run("What's the weather in Paris?")
# Access results
print(result.success) # True
print(result.answer) # "The weather in Paris is sunny."
print(result.events) # List of traced tool executions
PlannerAgent¶
Generates structured execution plans before acting.
from agenthelm import PlannerAgent
planner = PlannerAgent(name="planner", lm=lm, tools=[search, analyze, report])
plan = planner.plan("Analyze competitor pricing")
# Review the plan
print(plan.goal) # "Analyze competitor pricing strategies"
print(plan.reasoning) # LLM's reasoning
for step in plan.steps:
print(f" {step.id}: {step.tool_name} - {step.description}")
# Execution is handled by Orchestrator (Week 4)
AgentResult¶
All agents return an AgentResult with execution details:
result = agent.run("Do something")
result.success # bool - Did the agent complete successfully?
result.answer # str | None - Final answer
result.error # str | None - Error message if failed
result.events # list[Event] - All traced tool executions
result.total_cost_usd # float - Aggregated LLM cost
result.token_usage # TokenUsage - Aggregated tokens
result.iterations # int - Number of reasoning loops
Plan Schema¶
Plans are structured for parallel and sequential execution:
from agenthelm import Plan, PlanStep, StepStatus
plan = Plan(
goal="Research and summarize",
steps=[
PlanStep(id="1", tool_name="search", description="Find sources"),
PlanStep(id="2", tool_name="search", description="Find more sources"),
PlanStep(id="3", tool_name="summarize", description="Combine results",
depends_on=["1", "2"]), # Runs after 1 and 2 complete
]
)
# Steps 1 and 2 can run in parallel (no dependencies)
ready = plan.get_ready_steps() # Returns steps 1 and 2
# After completion
plan.mark_completed("1", result="source A")
plan.mark_completed("2", result="source B")
# Now step 3 is ready
ready = plan.get_ready_steps() # Returns step 3
Memory Integration¶
Agents can use the Memory Hub for context persistence:
from agenthelm import ToolAgent, MemoryHub
hub = MemoryHub(data_dir="./data")
agent = ToolAgent(
name="memory_agent",
lm=lm,
tools=[search],
memory=hub
)
# Agent can use memory internally
# await agent._remember("User prefers dark mode")
# results = await agent._recall("user preferences")
Tracing Integration¶
Agents automatically trace tool executions when given a tracer:
from agenthelm import ToolAgent, ExecutionTracer
from agenthelm.core.storage import SqliteStorage
tracer = ExecutionTracer(storage=SqliteStorage("traces.db"))
agent = ToolAgent(
name="traced_agent",
lm=lm,
tools=[my_tool],
tracer=tracer
)
result = agent.run("Do something")
# All tool calls are now persisted to traces.db
API Reference¶
BaseAgent ¶
Bases: ABC
Abstract base class for all agents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent identifier for tracing and registry |
required |
lm
|
LM
|
DSPy language model |
required |
tools
|
list[Callable] | None
|
List of tool functions the agent can use |
None
|
memory
|
MemoryHub | None
|
Optional MemoryHub for context persistence |
None
|
tracer
|
ExecutionTracer | None
|
Optional ExecutionTracer for tool call logging |
None
|
role
|
str | None
|
Optional role/persona description that influences behavior |
None
|
Source code in agenthelm/agent/base.py
options: show_source: false
ToolAgent ¶
Bases: BaseAgent
ReAct-style agent that reasons and executes tools.
Uses DSPy's ReAct pattern to iteratively: 1. Reason about the task 2. Choose and execute a tool 3. Observe the result 4. Repeat until done
Source code in agenthelm/agent/tool_agent.py
Attributes¶
Functions¶
run ¶
Execute the ReAct loop and return results with traced events.
Source code in agenthelm/agent/tool_agent.py
options: show_source: false
PlannerAgent ¶
Bases: BaseAgent
Agent that generates structured execution plans.
The PlannerAgent creates plans but does NOT execute them. Execution is handled by the Orchestrator (Week 4).
Source code in agenthelm/agent/planner.py
Attributes¶
Functions¶
plan ¶
Generate an execution plan for the given task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str
|
The task to plan for |
required |
Returns:
| Type | Description |
|---|---|
Plan
|
Plan object with steps (not yet executed) |
Source code in agenthelm/agent/planner.py
options: show_source: false
AgentResult ¶
Bases: BaseModel
Result of an agent execution.
Contains the final answer, execution events, and cost/token metrics.
Attributes¶
answer
class-attribute
instance-attribute
¶
error
class-attribute
instance-attribute
¶
events
class-attribute
instance-attribute
¶
iterations
class-attribute
instance-attribute
¶
session_id
class-attribute
instance-attribute
¶
success
class-attribute
instance-attribute
¶
token_usage
class-attribute
instance-attribute
¶
token_usage = Field(
default_factory=lambda: TokenUsage(
input_tokens=0, output_tokens=0
),
description="Aggregated token usage",
)
total_cost_usd
class-attribute
instance-attribute
¶
Functions¶
add_event ¶
Add an event and update aggregated metrics.
Source code in agenthelm/agent/result.py
options: show_source: false
Plan ¶
Bases: BaseModel
A structured execution plan with potentially parallel steps.
Steps with no dependencies can run in parallel. Steps with dependencies run after their dependencies complete.
Attributes¶
approved
class-attribute
instance-attribute
¶
goal
class-attribute
instance-attribute
¶
reasoning
class-attribute
instance-attribute
¶
steps
class-attribute
instance-attribute
¶
Functions¶
get_ready_steps ¶
Get all steps that are ready to execute (no pending dependencies).
Source code in agenthelm/agent/plan.py
get_step ¶
mark_completed ¶
mark_failed ¶
to_yaml ¶
Serialize plan to YAML for human review.
Source code in agenthelm/agent/plan.py
options: show_source: false
PlanStep ¶
Bases: BaseModel
A single step in an execution plan.
Steps can have dependencies for parallel/sequential execution. Supports Saga pattern with compensating actions on failure.
Attributes¶
agent_name
class-attribute
instance-attribute
¶
args
class-attribute
instance-attribute
¶
compensate_args
class-attribute
instance-attribute
¶
compensate_tool
class-attribute
instance-attribute
¶
compensate_tool = Field(
default=None,
description="Tool to run on rollback (overrides tool default)",
)
depends_on
class-attribute
instance-attribute
¶
description
class-attribute
instance-attribute
¶
error
class-attribute
instance-attribute
¶
result
class-attribute
instance-attribute
¶
status
class-attribute
instance-attribute
¶
tool_name
class-attribute
instance-attribute
¶
options: show_source: false