ai newsSo., 1. Nov.
EilmeldungPyTorch Explained: Machine Learning You Can Watch Learn

Your LLM Is Not an AI Agent: Build a Real AI Agent Using Python, Tool Calling, and APIs

**** An LLM can explain how to perform a task. But can it actually perform that task? This is where AI application engineering begins. We hear about AI agents, autonomous systems, agentic workflows, function calling, MCP, and intelligent automation almost every day. But there is an important engine

dev.toSonntag 12:22 · 01.11.5 Min. Lesezeit
Bild: dev.to

An LLM can explain how to perform a task. But can it actually perform that task?

This is where AI application engineering begins.

We hear about AI agents, autonomous systems, agentic workflows, function calling, MCP, and intelligent automation almost every day.

But there is an important engineering question:

How does an AI model interact with real applications, execute functions, retrieve information, and perform actions?

Let's build a working foundation instead of discussing AI agents only from a theoretical perspective.

In this article, we'll build a Python-based AI application where an LLM decides when to use a calculator, your Python application executes the function, and the result returns to the LLM.

By the end, you'll understand the architecture behind tool-calling AI applications.

"Calculate the total cost of 25 servers at $120 each."

A traditional LLM might respond with the calculated answer.

But an application-integrated AI system can do something more interesting:

An LLM does not automatically execute your Python functions. Your application is responsible for executing the requested function.

flowchart TD A[User Request] --> B[Python Application] B --> C[LLM API] C --> D{Tool Required?} D -->|Yes| E[Function Call] E --> F[Python Tool Executor] F --> G[Calculator Function] G --> H[Function Result] H --> C D -->|No| I[Final Response] C --> I I --> J[User]

Notice something important: the Python application controls execution.

The LLM selects the function, but the application decides whether and how to execute it.

ai-tool-calling/ │ ├── main.py ├── tools.py ├── requirements.txt ├──.env └── README.md

OPENAI_API_KEY=your_api_key OPENAI_MODEL=your_supported_model

Never hardcode API credentials in application source code or commit ".env" to Git.

operations = { "add": lambda: a + b, "subtract": lambda: a - b, "multiply": lambda: a * b, "divide": lambda: a / b } if operation not in operations: raise ValueError("Unsupported operation") if operation == "divide" and b == 0: raise ValueError("Division by zero is not allowed") return operations[operation]() Enter fullscreen mode Exit fullscreen mode We have implemented four operations.

Notice that we are not using Python's "eval()".

Because executing arbitrary model-generated expressions introduces unnecessary security risks.

This is one of the most important parts of tool calling.

calculator_tool = { "type": "function", "function": { "name": "calculator", "description": "Perform basic mathematical calculations", "parameters": { "type": "object", "properties": { "operation": { "type": "string", "enum": [ "add", "subtract", "multiply", "divide" ] }, "a": { "type": "number" }, "b": { "type": "number" } }, "required": ["operation", "a", "b"], "additionalProperties": False }, "strict": True } }

The schema describes the function to the model.

from dotenv import load_dotenv from openai import OpenAI

client = OpenAI( api_key=os.getenv("OPENAI_API_KEY") )

calculator_tool = { "type": "function", "function": { "name": "calculator", "description": "Perform mathematical calculations", "parameters": { "type": "object", "properties": { "operation": { "type": "string", "enum": ["add", "subtract", "multiply", "divide"] }, "a": {"type": "number"}, "b": {"type": "number"} }, "required": ["operation", "a", "b"], "additionalProperties": False }, "strict": True } }

if name!= "calculator": raise ValueError("Unknown tool") return calculator(**arguments) Enter fullscreen mode Exit fullscreen mode def ask_ai(user_question):

messages = [ { "role": "user", "content": user_question } ] response = client.chat.completions.create( model=MODEL, messages=messages, tools=[calculator_tool], tool_choice="auto" ) assistant_message = response.choices.message messages.append(assistant_message) if assistant_message.tool_calls: for tool_call in assistant_message.tool_calls: function_name = tool_call.function.name arguments = json.loads( tool_call.function.arguments ) result = execute_tool( function_name, arguments ) messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps({ "result": result }) }) final_response = client.chat.completions.create( model=MODEL, messages=messages, tools=[calculator_tool] ) return final_response.choices.message.content return assistant_message.content Enter fullscreen mode Exit fullscreen mode if name == " main ":

question = ( "Calculate the total cost of 25 servers " "at 120 dollars each." ) answer = ask_ai(question) print("\nAI Response:") print(answer) Enter fullscreen mode Exit fullscreen mode Execute the application Run:

User Request: Calculate the total cost of 25 servers at 120 dollars each.

Function Arguments: { "operation": "multiply", "a": 25, "b": 120 }

Python Execution: calculator("multiply", 25, 120)

Final AI Response: The total cost of 25 servers at $120 each is $3,000.

The exact wording of the final response can vary.

Let's understand the execution from an engineering perspective.

Component| Responsibility LLM| Understands the request Tool schema| Defines available capabilities Function selection| Identifies the required function Python| Executes the function Tool response| Returns execution output LLM| Generates the final response

This is the foundation of an application-integrated AI agent.

Production applications must handle failures.

Do not assume that every model-generated function call is valid or safe.

For operations that modify external systems, introduce explicit authorization and approval controls.

The calculator is only a starting point.

Imagine exposing multiple tools to the same LLM.

Tool| Capability Calculator| Mathematical operations Database query| Retrieve application data Weather API| Retrieve weather information Ticket API| Retrieve support tickets Inventory API| Check product availability File search| Retrieve document information Monitoring API| Retrieve system metrics

Now the LLM can select different application capabilities depending on the user's request.

This is where application engineering meets agentic AI.

A working demonstration is not automatically a production-ready AI agent.

Once you understand tool calling, you can progressively build more sophisticated applications.

Each stage introduces a new engineering challenge.

The biggest misconception about AI agents is that an LLM alone makes an application intelligent.

In reality, a useful AI application requires much more:

The future of AI application development is not just about writing better prompts. It is about engineering reliable systems around intelligent models.

I would like to hear from the developer community:

Share your experience in the comments. Let's learn from real implementations, not just AI terminology.

Suggested DEV.to tags: "#ai" "#python" "#llm" "#agents" "#tutorial"

Quelle: dev.to (dev.to). Original öffnen