I am trying to make a simple LLM agent that would be able to solve a terminal query (e.g. Find and list the contents of the file "cat.txt"). For this, I am also defining the functions to be used as tools, in an attempt to prompt my agent to generate function calls, whose result I can append as the next message to the agent to automatize the chat. However, from my test runs on this sample code, not only does my agent not follow the system instructions very well, it also seems to underuse the available tools. For example, the "cat" function/tool, is never called, even when the agent knows where the target file is located and the function call would suffice to solve the query. # Tool definitions in LiteLLM format tools = [ { "type": "function", "function": { "name": "ls", "description": "List files in a given directory.", "parameters": { "type": "object", "properties": { "directory": { "type": "string", "description": "The directory whose contents are to be listed e.g. /home/", } } }, }, }, { "type": "function", "function": { "name": "cd", "description": "Change the current directory. The path given to change must be absolute starting from root or relative from the current directory.", "parameters": { "type": "object", "properties": { "directory": { "type": "string", "description": "The target directory to change to.", } } }, }, }, { "type": "function", "function": { "name": "cat", "description": "Read the content of a file. The file must be readable and not a directory. If the file doesn't exist in the current directory, this command will return an error message.", "parameters": { "type": "object", "properties": { "filename": { "type": "string", "description": "The target file to be read.", } } }, }, }, ] # Function to handle tool execution def execute_tool(tool_name, args): if tool_name == "ls": return ls(**args) elif tool_name == "cd": return cd(**args) elif tool_name == "cat": return cat(**args) else: return json.dumps({"error": "Unknown tool."}) # Function to interact with the agent def run_agent(prompt): messages = [{"role": "system", "content": """You are a Linux terminal assistant. Use 'ls', 'cd', and 'cat' to locate files. You will be prompted to solve a simple terminal query. Use the provided tools do to so. Always first list the CURRENT directory using the 'ls' tool to find out which files and directories are present inside it. Once the query is solved, do not use any tool and respond with 'solved'. Do not request further input from the user. When you are asked to print the contents of a file, execute the cat command to retrieve its contents and then return those. Do NOT list the root directory or any parent directories. Work from the current directory and its children directories."""}] messages = [{"role": "user", "content": prompt}] lap = 1 while True: print(f"\nLap: {lap}") lap+=1 response = litellm.completion( model="gemini/gemini-2.0-flash", # Replace with an appropriate model messages=messages, tools=tools, tool_choice="auto", temperature=0.8, ) print(response) # Debugging: See full response structure # response = response.choices[0].message # print(response.choices[0].message.tool_calls[0].function.name) # print(response.choices[0].message.tool_calls[0].function.arguments) # Ensure tool_calls exists in response if hasattr(response.choices[0].message, "tool_calls"): for tool_call in response.choices[0].message.tool_calls: tool_name = tool_call.function.name tool_args = json.loads(tool_call.function.arguments) assert isinstance(tool_name, str), f"Expected str, got {type(tool_name)}" assert isinstance(tool_args, dict), f"Expected dict, got {type(tool_args)}" result = execute_tool(tool_name, tool_args) print(result) messages.append({"role": "assistant", "content": f"{tool_name}({tool_args}) -> {result}"}) else: print(f"No tool calls here...") # No more tool calls, so return the final response return response["choices"][0]["message"]["content"] # Example usage print(run_agent("Find the file 'key.txt' and print its contents.")) Continue reading...