Python / Python Modern Generative AI and Agents Interview Questions
What is an AI agent and how does function calling / tool use work in LLM-based agents?
An AI agent is a system where an LLM acts as a reasoning engine that decides what actions to take (calling tools, retrieving information, writing code) based on a goal, observes the results of those actions, and continues reasoning until the goal is met. Unlike a simple chain that executes a fixed sequence, an agent dynamically chooses which tools to invoke and in what order.
Modern LLMs (GPT-4, Claude, Gemini) support function calling (also called tool use): you define a set of tools with JSON schemas describing their parameters, and the model returns a structured JSON object specifying which tool to call and with what arguments — instead of (or in addition to) returning natural language. The application executes the function, returns the result to the model, and the model continues until it has enough information to answer.
from openai import OpenAI import json client = OpenAI() # Define tools with JSON schema tools = [ { 'type': 'function', 'function': { 'name': 'get_weather', 'description': 'Get current weather for a city', 'parameters': { 'type': 'object', 'properties': { 'city': {'type': 'string', 'description': 'City name'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}, }, 'required': ['city'], }, }, } ] def get_weather(city: str, unit: str = 'celsius') -> dict: return {'city': city, 'temp': 22, 'unit': unit, 'condition': 'Sunny'} messages = [{'role': 'user', 'content': 'What is the weather in Paris?'}] # First LLM call â model decides to call the tool response = client.chat.completions.create( model='gpt-4o', messages=messages, tools=tools, tool_choice='auto' ) msg = response.choices[0].message if msg.tool_calls: tool_call = msg.tool_calls[0] args = json.loads(tool_call.function.arguments) result = get_weather(**args) # execute the real function # Append model's tool call and the function result messages.append(msg) messages.append({ 'role': 'tool', 'tool_call_id': tool_call.id, 'content': json.dumps(result), }) # Second LLM call â model formulates final answer from tool result final = client.chat.completions.create( model='gpt-4o', messages=messages ) print(final.choices[0].message.content) # 'The current weather in Paris is 22°C and Sunny.'
More Related questions...