How to Intermediate · 3 min read

How to build agents without a framework

Quick answer
To build agents without a framework, use a language model like gpt-4o to process inputs, generate actions, and manage state manually in your code. Implement a loop that sends prompts, interprets responses, and executes tasks programmatically without relying on external agent libraries.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.

bash
pip install openai

Step by step

This example shows a simple agent that takes a user query, uses gpt-4o to decide an action, and executes it. The agent loops until the user says 'exit'.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def agent_loop():
    print("Simple agent started. Type 'exit' to quit.")
    while True:
        user_input = input("User: ")
        if user_input.lower() == "exit":
            print("Agent stopped.")
            break

        # Construct prompt for the agent
        prompt = f"You are an agent that decides what to do based on user input.\nUser says: {user_input}\nRespond with an action and explanation."

        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}]
        )

        agent_reply = response.choices[0].message.content
        print(f"Agent: {agent_reply}")

        # Here you could parse the agent_reply to trigger real actions

if __name__ == "__main__":
    agent_loop()
output
Simple agent started. Type 'exit' to quit.
User: What is the weather today?
Agent: Action: Search weather API. Explanation: The user wants to know the weather, so I should fetch current weather data.
User: exit
Agent stopped.

Common variations

You can extend this basic agent by adding:

  • Parsing the model's output to trigger API calls or system commands.
  • Maintaining conversation or task state in variables.
  • Using async calls for non-blocking I/O.
  • Switching models like claude-3-5-sonnet-20241022 for better coding or reasoning.
python
import asyncio
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def async_agent_loop():
    print("Async agent started. Type 'exit' to quit.")
    while True:
        user_input = input("User: ")
        if user_input.lower() == "exit":
            print("Agent stopped.")
            break

        prompt = f"You are an agent. User says: {user_input}. Respond with action and explanation."

        response = await client.chat.completions.acreate(
            model="claude-3-5-sonnet-20241022",
            messages=[{"role": "user", "content": prompt}]
        )

        agent_reply = response.choices[0].message.content
        print(f"Agent: {agent_reply}")

if __name__ == "__main__":
    asyncio.run(async_agent_loop())
output
Async agent started. Type 'exit' to quit.
User: Write a Python function to add two numbers.
Agent: Action: Write code. Explanation: The user wants a Python function, so I will generate it.
User: exit
Agent stopped.

Troubleshooting

If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly. For rate limits, add retries with exponential backoff. If the agent output is unclear, improve prompt clarity or add output parsing logic.

Key Takeaways

  • Use the OpenAI SDK to send prompts and receive responses for agent logic without frameworks.
  • Implement a loop to handle user input, model calls, and action execution manually.
  • Parse model outputs to trigger real-world actions or API calls.
  • Async calls improve responsiveness for I/O-bound agent tasks.
  • Clear prompts and output parsing are key to reliable agent behavior.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗