kodeagent.fca#
Function Calling Agent. This module is optimized for Small Language Models (SLMs). The FC agent uses native function calling to solve tasks. Some of the data structures are reproduced here to keep this module self-contained and optimized.
Provide the final answer to the user's task and end the conversation. |
|
Example usage of the FunctionCallingAgent. |
|
An agent that uses native function calling to solve tasks, optimized for Small Language Models (SLMs). |
Function Calling Agent. This module is optimized for Small Language Models (SLMs). The FC agent uses native function calling to solve tasks. Some of the data structures are reproduced here to keep this module self-contained and optimized.
- class kodeagent.fca.FunctionCallingAgent(model_name: str, tools: list[Callable] | None = None, system_prompt: str = "You are a helpful assistant that uses tools to solve tasks. Always use a tool.\nIf you have gathered enough information to answer the user's request, you MUST call the `final_answer` tool with your result.\nIf you have called the same tool multiple times without success, STOP and provide your best possible answer using the `final_answer` tool.\nDo not get stuck in a loop. If you cannot solve the task, explain why using the `final_answer` tool.\nUnless otherwise specified, answer in the same language as the users' task input.\n", loop_detection_threshold: int = 3, litellm_params: dict | None = None, max_tool_result_chars: int = 3000, one_line_doc: bool = True, tool_timeout: float = 30.0)[source]#
Bases:
objectAn agent that uses native function calling to solve tasks, optimized for Small Language Models (SLMs). If you’re using Ollama, make sure to select a model that supports function calling, such as ‘ollama/qwen3:8b-q8_0’.
Initialize the FunctionCallingAgent.
- Parameters:
model_name – Model identifier for LiteLLM.
tools – Optional list of callable tools.
system_prompt – System prompt for the agent.
loop_detection_threshold – Number of consecutive same tool calls before triggering loop detection. Default is 3.
litellm_params – Optional dictionary of parameters to pass to LiteLLM calls.
max_tool_result_chars – Maximum number of characters to store in chat history for each tool result. Longer results are truncated with a note. Full results are preserved separately for final answer preparation.
one_line_doc – If True, use only the first line of the tool’s docstring for the schema. This can help reduce token usage for SLMs. If False, the full docstring is used, which may provide more context but at the cost of more tokens.
tool_timeout – Seconds to wait for a single tool call before cancelling it and returning an error result. Default is 30.0.
- response(rtype: Literal['step', 'final', 'log'], value: Any, channel: str | None = None, metadata: dict[str, Any] | None = None) AgentResponse[source]#
Prepare a response to be sent by the agent.
- Parameters:
rtype – Response type emitted by the agent.
value – The current update from the agent.
channel – The response channel.
metadata – Any metadata associated with the update.
- Returns:
A response from the agent.
- async run(task: str, files: list[str] | None = None, max_iterations: int = 10, refine_final_answer: bool = True, use_planning: bool = True, recurrent_mode: bool = False, loop_threshold: int = 3, chat_history: list[dict] | None = None) AsyncIterator[AgentResponse][source]#
Main loop for the agent to process input and execute tools until finished. If an SLM call fails, it tries up to 3 times with a backoff before terminating.
- Parameters:
task – Task description to process. Add URLs or file contents in the task description.
files – An optional list of files related to the task.
max_iterations – Maximum number of iterations to run. Failed SLM retries do
do. (not count against this budget — only successful reasoning steps)
refine_final_answer – If True, calls an additional SLM step to produce a
final_answer. (clean final answer when the model exits without calling)
reliably. (Recommended for models <=4B that may not use final_answer)
Note – not invoked after a hard-stop due to repeated SLM failures.
use_planning – If True, generate a simple plan at the beginning of the task.
recurrent_mode – If True, the agent continues from the previous task result,
chat_history. (allowing for multi-turn workflows. Mutually exclusive with)
loop_threshold – Number of consecutive same tool calls before triggering loop detection.
chat_history – Optional pre-built OpenAI-compliant history to inject as the
end. (base context for this run. The new task message is appended at the)
recurrent_mode. (Mutually exclusive with)
- Yields:
AgentResponse – Streaming log, step, and final responses.
- Raises:
ValueError – If both
recurrent_modeandchat_historyare set, or if the provided history fails OpenAI compliance validation.
- kodeagent.fca.final_answer(result: str) str[source]#
Provide the final answer to the user’s task and end the conversation. Always call this tool when you have enough information to answer.
- Parameters:
result – The final answer or result of the task.
- Returns:
The final answer text in user-readable format.