{"slug": "how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of", "title": "How LLM Tool Calling Actually Works: Build an Agent From Scratch in 160 Lines of Python", "summary": "A developer explains that AI agents are fundamentally simple while loops that call an LLM, check for tool requests, execute them, and feed results back until the LLM can answer. The article demonstrates building a functional agent from scratch in 160 lines of Python using only the OpenAI SDK, stripping away the complexity of frameworks like LangChain and CrewAI.", "body_md": "Member-only story\n\n# How LLM Tool Calling Actually Works: Build an Agent From Scratch in 160 Lines of Python\n\n## No LangChain. No CrewAI. Just the loop that every agent framework is hiding from you.\n\nI used to think AI agents were complicated. The frameworks certainly make them look complicated chains, executors, graphs, memory modules, callback managers. I spent a weekend inside one framework’s source code trying to figure out why my agent was calling the wrong tool, and by the end I had learned something I didn’t expect:\n\n**An agent is a while loop.**\n\nThat’s it. That’s the whole secret. A loop that calls an LLM, checks whether the LLM asked to run a function, runs it, feeds the result back, and repeats until the LLM has enough information to answer.\n\nEverything else, the chains, the graphs, the abstractions is packaging around that loop. Useful packaging sometimes. But if you’ve never seen the bare loop, you’re using agents the way most people use cars: fine until something breaks, and then you’re helpless.\n\nSo let’s build one. Around 160 lines of Python, one dependency (the OpenAI SDK, which is just an API client, not a framework), and by the end you’ll understand exactly what happens when any agent “decides” to use a tool.", "url": "https://wpnews.pro/news/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of", "canonical_source": "https://pub.towardsai.net/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of-python-8df10d0e1109?source=rss----98111c9905da---4", "published_at": "2026-07-10 05:27:05+00:00", "updated_at": "2026-07-10 05:39:47.539296+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-agents", "developer-tools"], "entities": ["OpenAI", "LangChain", "CrewAI"], "alternates": {"html": "https://wpnews.pro/news/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of", "markdown": "https://wpnews.pro/news/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of.md", "text": "https://wpnews.pro/news/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of.txt", "jsonld": "https://wpnews.pro/news/how-llm-tool-calling-actually-works-build-an-agent-from-scratch-in-160-lines-of.jsonld"}}