How to Build AI Agents
Complete Guide for Developers

Architectures, frameworks, code examples, and best practices. Everything you need to go from "what is an agent" to shipping one in production.

What are AI agents?

A chatbot answers questions. An AI agent completes tasks. That's the fundamental difference, and it matters more than you'd think.

When you ask ChatGPT "write me a script to organize my downloads folder," it gives you the code. You copy it, save it, run it, fix the errors, run it again. When you ask an AI agent the same thing, it writes the code, executes it, reads the error output, fixes the code, and runs it again — all without you touching the keyboard.

The core mechanism is the agent loop, sometimes called the perceive-reason-act-observe cycle:

1. PERCEIVE: Receive input (user message, file change, scheduled trigger)
2. REASON:   Decide what to do next (using the LLM)
3. ACT:      Execute an action (call a tool, run code, browse a page)
4. OBSERVE:  Read the result of the action
5. REPEAT:   Go back to step 2 until the task is done

This loop is what makes agents powerful. Instead of a single request-response, you get autonomous execution that can handle multi-step workflows, recover from errors, and adapt to unexpected results. The model becomes a decision-maker, not just a text generator.

Agent architectures

Five patterns that cover 95% of agent use cases. Start with the first one.

01

Single-Agent (Tool Use)

Beginner

One model with a set of tools. The model decides which tool to call, gets the result, and continues. This is the most common pattern and what most people mean by 'AI agent'.

02

ReAct (Reason + Act)

Beginner

The model explicitly reasons about each step before acting. Thought → Action → Observation → repeat. Forces transparent, debuggable decision-making.

03

Planner-Executor

Intermediate

One model creates a plan (list of steps), another executes each step. Separates strategy from execution, allowing different models for each.

04

Multi-Agent

Advanced

Multiple agents with different roles (researcher, coder, reviewer) collaborate on a task. Powerful but harder to orchestrate and debug.

05

Hierarchical

Advanced

A manager agent delegates sub-tasks to worker agents. Good for complex workflows but adds latency and cost at each delegation layer.

The ReAct loop in code

async function agentLoop(task: string, tools: Tool[]) {
  let messages = [
    { role: "system", content: "You are a helpful agent. Use tools to complete tasks." },
    { role: "user", content: task }
  ];

  while (true) {
    // REASON: Ask the model what to do next
    const response = await llm.chat({
      messages,
      tools: tools.map(t => t.schema),
    });

    // If the model gives a final answer, we're done
    if (response.content && !response.tool_calls) {
      return response.content;
    }

    // ACT: Execute each tool call
    for (const call of response.tool_calls ?? []) {
      const tool = tools.find(t => t.name === call.name);
      const result = await tool.execute(call.arguments);

      // OBSERVE: Feed the result back to the model
      messages.push({
        role: "tool",
        tool_call_id: call.id,
        content: JSON.stringify(result),
      });
    }
  }
}

Tools and frameworks

You don't have to build from scratch, but you can. Here's the landscape.

FrameworkPros
LangChainPython, JSHuge ecosystem, lots of integrations, well-documented
CrewAIPythonClean API, role-based agents, easy to reason about
AutoGenPythonMicrosoft-backed, strong code agent patterns
CopperRiverTypeScript (built-in)Zero setup, browser+terminal+files built-in, open-source models, $9/mo
Custom (OpenAI SDK)AnyNo framework lock-in, minimal dependencies, maximum control

If you're building a backend service, LangChain or a custom implementation is the way to go. If you want a desktop agent that can browse, code, and automate without writing infrastructure — CopperRiver handles all of that out of the box.

Building your first agent

Let's build a research agent that takes a question, searches the web, reads relevant pages, and returns a cited summary. We'll use the OpenAI SDK pattern — it works with any model provider.

Step 1: Define your tools

Tools are just functions with a schema. The model reads the schema and decides when to call them.

const tools = [{
  type: "function",
  function: {
    name: "web_search",
    description: "Search the web for current information",
    parameters: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search query" }
      },
      required: ["query"]
    }
  }
}, {
  type: "function",
  function: {
    name: "read_page",
    description: "Read the content of a web page",
    parameters: {
      type: "object",
      properties: {
        url: { type: "string", description: "URL to read" }
      },
      required: ["url"]
    }
  }
}];

Step 2: Implement the tool functions

async function executeTool(name: string, args: any) {
  switch (name) {
    case "web_search":
      return await searchWeb(args.query);
    case "read_page":
      return await fetchPageContent(args.url);
    default:
      throw new Error(`Unknown tool: ${name}`);
  }
}

Step 3: Run the agent loop

const messages = [
  { role: "system", content: "You are a research agent. Search for information, read sources, and provide cited answers." },
  { role: "user", content: "What are the latest open-source AI models released this month?" }
];

// Agent loop
for (let i = 0; i < 10; i++) {  // max 10 steps
  const response = await client.chat.completions.create({
    model: "deepseek-v4",
    messages,
    tools,
  });

  const msg = response.choices[0].message;
  messages.push(msg);

  if (!msg.tool_calls?.length) break;  // done

  for (const call of msg.tool_calls) {
    const result = await executeTool(call.function.name, JSON.parse(call.function.arguments));
    messages.push({ role: "tool", tool_call_id: call.id, content: result });
  }
}

The result

That's it. The agent will search, read pages, and synthesize an answer — autonomously. The same pattern works with any OpenAI-compatible endpoint, including open-source models running locally via vLLM or CopperRiver.

Common agent patterns

RAG Agent

Combines document retrieval with tool use. The agent searches a vector database, reads relevant chunks, and answers with citations. Best for knowledge-base chatbots and document Q&A.

Browser Automation Agent

Navigates websites, fills forms, clicks buttons, extracts data. Tools include navigate, click, type, screenshot. CopperRiver has this built-in via CDP.

Coding Agent

Reads code, writes code, runs tests, fixes errors. Tools include file read/write, terminal execution, and code search. Can work on real codebases.

Research Agent

Searches the web, reads multiple sources, cross-references facts, and produces a cited report. The example above is a simplified version.

Challenges and best practices

Error handling: Tools fail. APIs time out. Models hallucinate function calls. Always wrap tool execution in try/catch and feed errors back to the model so it can recover.
Cost management: Agents make many LLM calls per task. Set a maximum step count, use cheaper models for simple steps, and consider caching. Open-source models locally eliminate per-token costs.
Context window limits: Long agent loops accumulate tokens fast. Trim old observations, summarize intermediate results, or use models with large context windows (128K+).
Evaluation: Test agents on a fixed set of tasks and measure success rate, cost, and time. Without evals, you're flying blind.

Frequently asked questions

What is an AI agent?

An AI agent is a system that uses a language model to perceive input, reason about what to do, take actions like calling APIs or running code, and observe the results in an autonomous loop. Unlike a chatbot, an agent can complete multi-step tasks on its own.

What frameworks are best for building AI agents?

Popular frameworks include LangChain, CrewAI, AutoGen, and custom implementations. For desktop automation agents, CopperRiver provides a ready-to-use agent runtime with browser, terminal, and file access starting at $9/month.

How much does it cost to build an AI agent?

API-based agents can cost $0.50-$5 per task. Using open-source models locally via CopperRiver or vLLM reduces costs significantly, with plans starting at $9/month.

What is the difference between a chatbot and an AI agent?

A chatbot responds to messages with text. An AI agent can take real actions — browse websites, run code, manipulate files, call APIs — to complete tasks autonomously.

Do AI agents work with open-source models?

Yes. Open-source models like GLM-5.2, DeepSeek V4, and Qwen 3.6 support tool calling, which is the foundation of agents. CopperRiver uses these models for its built-in agent capabilities.

Ship agents, not infrastructure

CopperRiver gives you a production-ready agent runtime with browser, terminal, file access, and open-source models. No framework setup required. Plans from $9/mo.