agent
Agentic execution: tool-call loops, result types, and response validation.
Agentic execution: tool-call loops, result types, and response validation.
- class AgentLoop(client, tool_registry, max_rounds=5, tool_timeout=30.0, continue_on_tool_error=True)[source]
Bases:
objectExecutes agentic tool-call loops for a single prompt.
- Usage:
loop = AgentLoop(client, tool_registry, max_rounds=5) result = loop.execute(
prompt=”Search for information about X”, tools=[“search_tool”, “calculate”],
)
- Parameters:
client (FFAIClientBase)
tool_registry (ToolRegistry)
max_rounds (int)
tool_timeout (float)
continue_on_tool_error (bool)
- __init__(client, tool_registry, max_rounds=5, tool_timeout=30.0, continue_on_tool_error=True)[source]
Initialize the agent loop.
- Parameters:
client (FFAIClientBase) – AI client to use for LLM calls.
tool_registry (ToolRegistry) – Registry of tools available for execution.
max_rounds (int) – Maximum number of tool-call rounds.
tool_timeout (float) – Timeout in seconds for individual tool execution.
continue_on_tool_error (bool) – Whether to continue the loop if a tool fails.
- Return type:
None
- execute(prompt, tools, tool_choice='auto', **kwargs)[source]
Execute the agentic loop.
Calls the LLM with the prompt and available tools. If the response contains tool calls, executes them and feeds results back. Repeats until no tool calls or max_rounds is reached.
- Parameters:
- Returns:
AgentResult with response, tool call records, and round/LLM counts.
- Return type:
- class AgentResult(response='', tool_calls=<factory>, total_rounds=0, total_llm_calls=0, status='success')[source]
Bases:
objectResult of an agentic execution loop.
- Parameters:
- tool_calls
List of tool call records from all rounds.
- status
Execution status - “success”, “failed”, or “max_rounds_exceeded”.
- Type:
Literal[‘success’, ‘failed’, ‘max_rounds_exceeded’]
- tool_calls: list[ToolCallRecord]
- property failed_tool_calls: list[ToolCallRecord]
List of tool calls that resulted in errors.
- class ResponseValidator(client, model=None, temperature=0.1)[source]
Bases:
objectValidates LLM responses using a second LLM as judge.
- Usage:
validator = ResponseValidator(client) result = validator.validate(
response=”The answer is 42”, criteria=”Must provide a numerical answer”,
) if result.passed:
print(“Response is valid”)
- Parameters:
client (FFAIClientBase) – FFAIClientBase used for validation LLM calls.
model (str | None) – Model to use for validation (defaults to client model).
temperature (float) – Temperature for validation calls (low for consistency).
- validate(response, criteria, max_retries=2, re_execute_fn=None)[source]
Validate a response against criteria.
- Parameters:
response (str) – The response text to validate.
criteria (str) – Validation criteria description.
max_retries (int) – Maximum re-execution attempts on failure.
re_execute_fn (Any | None) – Optional callable that accepts an augmented prompt and returns a new response string. If provided, failed validations trigger re-execution with the rejection reason.
- Returns:
ValidationResult with pass/fail status and critique.
- Return type:
- class ToolCallRecord(round, tool_name, tool_call_id='', arguments=<factory>, result='', duration_ms=0.0, error=None)[source]
Bases:
objectRecord of a single tool call within an agentic loop.
- Parameters:
Classes
- class AgentLoop(client, tool_registry, max_rounds=5, tool_timeout=30.0, continue_on_tool_error=True)[source]
Bases:
objectExecutes agentic tool-call loops for a single prompt.
- Usage:
loop = AgentLoop(client, tool_registry, max_rounds=5) result = loop.execute(
prompt=”Search for information about X”, tools=[“search_tool”, “calculate”],
)
- Parameters:
client (FFAIClientBase)
tool_registry (ToolRegistry)
max_rounds (int)
tool_timeout (float)
continue_on_tool_error (bool)
- __init__(client, tool_registry, max_rounds=5, tool_timeout=30.0, continue_on_tool_error=True)[source]
Initialize the agent loop.
- Parameters:
client (FFAIClientBase) – AI client to use for LLM calls.
tool_registry (ToolRegistry) – Registry of tools available for execution.
max_rounds (int) – Maximum number of tool-call rounds.
tool_timeout (float) – Timeout in seconds for individual tool execution.
continue_on_tool_error (bool) – Whether to continue the loop if a tool fails.
- Return type:
None
- execute(prompt, tools, tool_choice='auto', **kwargs)[source]
Execute the agentic loop.
Calls the LLM with the prompt and available tools. If the response contains tool calls, executes them and feeds results back. Repeats until no tool calls or max_rounds is reached.
- Parameters:
- Returns:
AgentResult with response, tool call records, and round/LLM counts.
- Return type:
- class AgentResult(response='', tool_calls=<factory>, total_rounds=0, total_llm_calls=0, status='success')[source]
Bases:
objectResult of an agentic execution loop.
- Parameters:
- tool_calls
List of tool call records from all rounds.
- status
Execution status - “success”, “failed”, or “max_rounds_exceeded”.
- Type:
Literal[‘success’, ‘failed’, ‘max_rounds_exceeded’]
- tool_calls: list[ToolCallRecord]
- property failed_tool_calls: list[ToolCallRecord]
List of tool calls that resulted in errors.
- class ResponseValidator(client, model=None, temperature=0.1)[source]
Bases:
objectValidates LLM responses using a second LLM as judge.
- Usage:
validator = ResponseValidator(client) result = validator.validate(
response=”The answer is 42”, criteria=”Must provide a numerical answer”,
) if result.passed:
print(“Response is valid”)
- Parameters:
client (FFAIClientBase) – FFAIClientBase used for validation LLM calls.
model (str | None) – Model to use for validation (defaults to client model).
temperature (float) – Temperature for validation calls (low for consistency).
- validate(response, criteria, max_retries=2, re_execute_fn=None)[source]
Validate a response against criteria.
- Parameters:
response (str) – The response text to validate.
criteria (str) – Validation criteria description.
max_retries (int) – Maximum re-execution attempts on failure.
re_execute_fn (Any | None) – Optional callable that accepts an augmented prompt and returns a new response string. If provided, failed validations trigger re-execution with the rejection reason.
- Returns:
ValidationResult with pass/fail status and critique.
- Return type: