{"slug": "making-local-ai-tool-calls-more-reliable", "title": "Making Local AI Tool Calls More Reliable", "summary": "A developer improved the reliability of local AI tool calls in an assistant by adding a recovery step that retries with tool_choice=\"required\" when the model fails to call a required tool. The fix includes buffering the first response to avoid showing ungrounded answers and was tested with regression tests and a local Gemma model.", "body_md": "Making Local AI Tool Calls More Reliable\n\nWhile testing our local-first AI assistant, I found an intermittent problem: the model sometimes answered with plain text instead of calling the tool needed to read or update local data.\n\nThe original system used `tool_choice=\"auto\"`\n\n. This normally worked, but it allowed the model to skip a required tool call. A prompt can guide a model, but it cannot guarantee that the model will always follow the tool protocol.\n\nI fixed this by adding a safe recovery step. The first request still uses automatic tool selection, so normal conversation works as before. If an explicit local-data request returns no tool call, the assistant retries once with `tool_choice=\"required\"`\n\n. The retry happens only before any tool has run, which prevents duplicate database writes.\n\nThe assistant also checks whether the recovered tool belongs to the correct read or write group. During streaming, it buffers the first response so an incorrect, ungrounded answer is not shown before recovery completes.\n\nI tested the change with automated regression tests and the running local Gemma model. The live recovery flow was:\n\n``` php\nauto -> required -> auto\n```\n\nThe main lesson was simple: prompts describe expected behavior, but reliable agent systems also need program-level checks around model decisions.", "url": "https://wpnews.pro/news/making-local-ai-tool-calls-more-reliable", "canonical_source": "https://dev.to/alaindevs/making-local-ai-tool-calls-more-reliable-54bb", "published_at": "2026-08-21 14:33:57+00:00", "updated_at": "2026-08-21 14:46:41.800191+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "ai-tools", "developer-tools"], "entities": ["Gemma"], "alternates": {"html": "https://wpnews.pro/news/making-local-ai-tool-calls-more-reliable", "markdown": "https://wpnews.pro/news/making-local-ai-tool-calls-more-reliable.md", "text": "https://wpnews.pro/news/making-local-ai-tool-calls-more-reliable.txt", "jsonld": "https://wpnews.pro/news/making-local-ai-tool-calls-more-reliable.jsonld"}}