Skip to main content

What is an Agent?

An AI agent is a system that can:
  • Understand user goals
  • Plan actions to achieve goals
  • Use tools and APIs
  • Make decisions autonomously
  • Learn from feedback

Basic Agent Architecture

import os
import json
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["CHEAPESTINFERENCE_API_KEY"],
    base_url="https://api.cheapestinference.ai/v1",
)

def run_agent(user_input):
    messages = [
        {
            "role": "system",
            "content": "You are a helpful agent. Use tools to help the user."
        },
        {"role": "user", "content": user_input}
    ]
    
    tools = [
        {
            "type": "function",
            "function": {
                "name": "search_web",
                "description": "Search the web for information"
            }
        },
        {
            "type": "function",
            "function": {
                "name": "calculate",
                "description": "Perform calculations"
            }
        }
    ]
    
    # Agent loop
    max_iterations = 5
    for i in range(max_iterations):
        response = client.chat.completions.create(
            model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
            messages=messages,
            tools=tools
        )
        
        message = response.choices[0].message
        messages.append(message)
        
        # If no tool calls, agent is done
        if not message.tool_calls:
            return message.content
        
        # Execute tools
        for tool_call in message.tool_calls:
            result = execute_tool(tool_call)
            messages.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "name": tool_call.function.name,
                "content": json.dumps(result)
            })
    
    return "Max iterations reached"

# Use the agent
response = run_agent("What's the weather in Paris and how far is it from London?")
print(response)