API Reference

Uses Tools in a loop to solve complex tasks.

Module haystack_experimental.components.agents.agent

Agent

A Haystack component that implements a tool-using agent with provider-agnostic chat model support.

The component processes messages and executes tools until a exit_condition condition is met. The exit_condition can be triggered either by a direct text response or by invoking a specific designated tool.

Usage example

from haystack.dataclasses import ChatMessage
from haystack.tools.tool import Tool

tools = [Tool(name="calculator", description="..."), Tool(name="search", description="...")]

agent = Agent(
    chat_generator=OpenAIChatGenerator(),
    tools=tools,
    exit_condition="search",
)

# Run the agent
result = agent.run(
    messages=[ChatMessage.from_user("Find information about Haystack")]
)

assert "messages" in result  # Contains conversation history

Agent.__init__

def __init__(chat_generator: Union[OpenAIChatGenerator,
                                   "AnthropicChatGenerator"],
             tools: Optional[List[Tool]] = None,
             system_prompt: Optional[str] = None,
             exit_condition: str = "text",
             state_schema: Optional[Dict[str, Any]] = None,
             max_runs_per_component: int = 100,
             raise_on_tool_invocation_failure: bool = False)

Initialize the agent component.

Arguments:

  • chat_generator: An instance of the chat generator that your agent should use.
  • tools: List of Tool objects available to the agent
  • system_prompt: System prompt for the agent.
  • exit_condition: Either "text" if the agent should return when it generates a message without tool calls or the name of a tool that will cause the agent to return once the tool was executed
  • state_schema: The schema for the runtime state used by the tools.
  • max_runs_per_component: Maximum number of runs per component. Agent will raise an exception if a component exceeds the maximum number of runs per component.
  • raise_on_tool_invocation_failure: Should the agent raise an exception when a tool invocation fails? If set to False, the exception will be turned into a chat message and passed to the LLM.

Agent.warm_up

def warm_up() -> None

Warm up the Agent.

Agent.to_dict

def to_dict() -> Dict[str, Any]

Serialize the component to a dictionary.

Returns:

Dictionary with serialized data

Agent.from_dict

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Agent"

Deserialize the agent from a dictionary.

Arguments:

  • data: Dictionary to deserialize from

Returns:

Deserialized agent

Agent.run

def run(messages: List[ChatMessage], **kwargs) -> Dict[str, Any]

Process messages and execute tools until the exit condition is met.

Arguments:

  • messages: List of chat messages to process
  • kwargs: Additional keyword arguments matching the defined input types

Returns:

Dictionary containing messages and outputs matching the defined output types