Write Better AI Agent Tools: 5 Python Patterns LLMs Actually Understand
Your AI agent keeps picking the wrong tool. You tweak the system prompt. You add "IMPORTANT: use search_database, NOT search_web." It helps for a day, then breaks again. The problem is not the mode...
Source: DEV Community
Your AI agent keeps picking the wrong tool. You tweak the system prompt. You add "IMPORTANT: use search_database, NOT search_web." It helps for a day, then breaks again. The problem is not the model. The problem is your tool definitions. LLMs decide which tool to call based on three things: the tool name, the description, and the parameter schema. Most developers write tools like they are writing functions for other developers. Short names, minimal docstrings, stringly-typed parameters. The LLM gets a vague menu and guesses. These five patterns fix that. Each one is a specific change to how you define tools in Python — with working code you can adapt today. Pattern 1: Docstrings Are the Tool's Manual The LLM reads your docstring to decide when to call your tool. A one-line docstring forces the model to guess from the function name alone. Bad tool definition: from langchain_core.tools import tool @tool def search(query: str) -> str: """Search for information.""" # implementation retu