DEV Community

Dinesh_gowtham
Dinesh_gowtham

Posted on

How AI Agents Work: The Plan-Act-Observe Loop, Explained Step by Step

Imagine having a personal coding assistant that not only writes code for you but also debugs and optimizes it. This is now a reality with AI agents. But have you ever wondered how they actually work? In this post, we will dive into the plan-act-observe loop and explore how AI agents are changing the game for software development.

Introduction to AI Agents

An AI agent is a program that uses artificial intelligence to perform tasks on its own. Think of it like a personal assistant that can help you with various tasks, such as scheduling appointments or sending emails. In the context of software development, AI agents can help with tasks like code reviews, debugging, and optimization. But before we can understand how they work, we need to define what an AI agent is and what it can do.

An AI agent is a program that uses artificial intelligence to perform tasks on its own, making it a valuable tool for software development.

To get started with AI agents, we need to install the required SDK. For this example, we will use the @aws-sdk/lambda package to create an AI agent that automates code reviews and debugging.

// Import the required package
import { LambdaClient, CreateFunctionCommand } from "@aws-sdk/client-lambda";

// Create a new Lambda client
const lambdaClient = new LambdaClient({ region: "us-east-1" });

// Create a new Lambda function
const createFunctionCommand = new CreateFunctionCommand({
  FunctionName: "ai-agent",
  Runtime: "nodejs18.x",
  Role: "arn:aws:iam::123456789012:role/lambda-execution-role",
  Handler: "index.handler",
  Code: {
    ZipFile: Buffer.from("exports.handler = async (event) => { return { statusCode: 200 }; };", "utf8"),
  },
});

// Create the Lambda function
lambdaClient.send(createFunctionCommand).then((data) => {
  console.log(data);
}).catch((err) => {
  console.log(err);
});
Enter fullscreen mode Exit fullscreen mode

To get started with AI agents, you need to install the required SDK and create a new Lambda function.

The Plan-Act-Observe Loop

The plan-act-observe loop is a fundamental concept in AI agents. It's a cycle that consists of three stages: planning, acting, and observing. Think of it like a chef who needs to plan a meal, act on the plan by cooking the meal, and observe the result to see if it's satisfactory.

  1. Planning: In this stage, the AI agent determines what action to take based on its goals and the current state of the environment.
  2. Acting: In this stage, the AI agent takes the planned action and executes it.
  3. Observing: In this stage, the AI agent observes the result of its action and uses this information to update its plan.

Here's an example of how the plan-act-observe loop works in the context of code reviews:

// Define the planning stage
function planCodeReview(code) {
  // Determine what actions to take based on the code
  const actions = [];
  if (code.includes("console.log")) {
    actions.push("remove console.log statements");
  }
  return actions;
}

// Define the acting stage
function actOnCodeReview(actions, code) {
  // Take the planned actions and execute them
  let updatedCode = code;
  for (const action of actions) {
    if (action === "remove console.log statements") {
      updatedCode = updatedCode.replace(/console.log\(.*\);/g, "");
    }
  }
  return updatedCode;
}

// Define the observing stage
function observeCodeReview(updatedCode) {
  // Observe the result of the action and update the plan
  const observations = [];
  if (updatedCode.includes("error")) {
    observations.push("fix error");
  }
  return observations;
}

// Create a new code review agent
function codeReviewAgent(code) {
  const actions = planCodeReview(code);
  const updatedCode = actOnCodeReview(actions, code);
  const observations = observeCodeReview(updatedCode);
  return observations;
}
Enter fullscreen mode Exit fullscreen mode

The plan-act-observe loop is a fundamental concept in AI agents that consists of planning, acting, and observing stages.

Building a Simple AI Agent

To build a simple AI agent, we need to define its goals and the environment it will operate in. In the context of software development, the goal of an AI agent might be to automate code reviews and debugging. The environment might be a Lambda function that receives code updates and sends feedback to the developer.

Here's an example of how to build a simple AI agent using the @aws-sdk/lambda package:

// Import the required package
import { LambdaClient, UpdateFunctionCodeCommand } from "@aws-sdk/client-lambda";

// Create a new Lambda client
const lambdaClient = new LambdaClient({ region: "us-east-1" });

// Define the AI agent's goals and environment
function aiAgent(code) {
  // Determine what actions to take based on the code
  const actions = [];
  if (code.includes("console.log")) {
    actions.push("remove console.log statements");
  }

  // Take the planned actions and execute them
  let updatedCode = code;
  for (const action of actions) {
    if (action === "remove console.log statements") {
      updatedCode = updatedCode.replace(/console.log\(.*\);/g, "");
    }
  }

  // Observe the result of the action and update the plan
  const observations = [];
  if (updatedCode.includes("error")) {
    observations.push("fix error");
  }

  // Update the Lambda function code
  const updateFunctionCodeCommand = new UpdateFunctionCodeCommand({
    FunctionName: "ai-agent",
    ZipFile: Buffer.from(updatedCode, "utf8"),
  });

  // Update the Lambda function
  lambdaClient.send(updateFunctionCodeCommand).then((data) => {
    console.log(data);
  }).catch((err) => {
    console.log(err);
  });

  return observations;
}
Enter fullscreen mode Exit fullscreen mode

To build a simple AI agent, you need to define its goals and environment, and use the plan-act-observe loop to take actions and observe the results.

Real-World Applications of AI Agents

AI agents have many real-world applications in software development, such as automating code reviews, debugging, and optimization. They can also be used to automate testing, deployment, and monitoring of software applications.

Here's an example of how to use an AI agent to automate code reviews:

// Import the required package
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

// Create a new Lambda client
const lambdaClient = new LambdaClient({ region: "us-east-1" });

// Define the AI agent's goals and environment
function codeReviewAgent(code) {
  // Determine what actions to take based on the code
  const actions = [];
  if (code.includes("console.log")) {
    actions.push("remove console.log statements");
  }

  // Take the planned actions and execute them
  let updatedCode = code;
  for (const action of actions) {
    if (action === "remove console.log statements") {
      updatedCode = updatedCode.replace(/console.log\(.*\);/g, "");
    }
  }

  // Observe the result of the action and update the plan
  const observations = [];
  if (updatedCode.includes("error")) {
    observations.push("fix error");
  }

  // Invoke the Lambda function
  const invokeCommand = new InvokeCommand({
    FunctionName: "ai-agent",
    Payload: updatedCode,
  });

  // Invoke the Lambda function
  lambdaClient.send(invokeCommand).then((data) => {
    console.log(data);
  }).catch((err) => {
    console.log(err);
  });

  return observations;
}
Enter fullscreen mode Exit fullscreen mode

AI agents can be used to automate code reviews, debugging, and optimization, making them a valuable tool for software development.

The Future of Software Development

The future of software development will be shaped by AI agents and their ability to automate tasks, improve code quality, and reduce development time. As AI agents become more advanced, they will be able to take on more complex tasks, such as automating testing, deployment, and monitoring of software applications.

However, there are also potential risks and challenges associated with using AI agents, such as the risk of errors and exceptions in AI agent workflows. To handle these risks, developers need to implement error handling mechanisms and monitor AI agent performance.

Here's an example of how to handle errors and exceptions in AI agent workflows:

// Import the required package
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

// Create a new Lambda client
const lambdaClient = new LambdaClient({ region: "us-east-1" });

// Define the AI agent's goals and environment
function aiAgent(code) {
  try {
    // Determine what actions to take based on the code
    const actions = [];
    if (code.includes("console.log")) {
      actions.push("remove console.log statements");
    }

    // Take the planned actions and execute them
    let updatedCode = code;
    for (const action of actions) {
      if (action === "remove console.log statements") {
        updatedCode = updatedCode.replace(/console.log\(.*\);/g, "");
      }
    }

    // Observe the result of the action and update the plan
    const observations = [];
    if (updatedCode.includes("error")) {
      observations.push("fix error");
    }

    // Invoke the Lambda function
    const invokeCommand = new InvokeCommand({
      FunctionName: "ai-agent",
      Payload: updatedCode,
    });

    // Invoke the Lambda function
    lambdaClient.send(invokeCommand).then((data) => {
      console.log(data);
    }).catch((err) => {
      console.log(err);
    });
  } catch (err) {
    console.log(err);
  }
}
Enter fullscreen mode Exit fullscreen mode

To handle errors and exceptions in AI agent workflows, developers need to implement error handling mechanisms and monitor AI agent performance.

The Takeaway

Here are the key takeaways from this post:

  • AI agents are programs that use artificial intelligence to perform tasks on their own.
  • The plan-act-observe loop is a fundamental concept in AI agents that consists of planning, acting, and observing stages.
  • AI agents can be used to automate code reviews, debugging, and optimization, making them a valuable tool for software development.
  • To build a simple AI agent, you need to define its goals and environment, and use the plan-act-observe loop to take actions and observe the results.
  • AI agents have many real-world applications in software development, such as automating testing, deployment, and monitoring of software applications.
  • To handle errors and exceptions in AI agent workflows, developers need to implement error handling mechanisms and monitor AI agent performance.

Transparency notice

This article was written with the help of an AI system — Groq (LLaMA 3.3 70B).
The topic was scouted from live AWS and Node.js ecosystem signals, and the content —
including all code examples — was written autonomously without human editing.

Published: 2026-08-04 · Primary focus: AIAgents

All code blocks are intended to be correct and runnable, but please verify them

Find an error? Drop a comment — corrections are always welcome.

Top comments (0)