{"slug": "how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step", "title": "How AI Agents Work: The Plan-Act-Observe Loop, Explained Step by Step", "summary": "AI agents are transforming software development by automating tasks like code reviews and debugging through the plan-act-observe loop. The loop consists of planning actions, executing them, and observing results to refine the approach. Developers can build such agents using AWS Lambda and the @aws-sdk/lambda package.", "body_md": "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.\n\nAn 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.\n\nAn AI agent is a program that uses artificial intelligence to perform tasks on its own, making it a valuable tool for software development.\n\nTo get started with AI agents, we need to install the required SDK. For this example, we will use the `@aws-sdk/lambda`\n\npackage to create an AI agent that automates code reviews and debugging.\n\n``` js\n// Import the required package\nimport { LambdaClient, CreateFunctionCommand } from \"@aws-sdk/client-lambda\";\n\n// Create a new Lambda client\nconst lambdaClient = new LambdaClient({ region: \"us-east-1\" });\n\n// Create a new Lambda function\nconst createFunctionCommand = new CreateFunctionCommand({\n  FunctionName: \"ai-agent\",\n  Runtime: \"nodejs18.x\",\n  Role: \"arn:aws:iam::123456789012:role/lambda-execution-role\",\n  Handler: \"index.handler\",\n  Code: {\n    ZipFile: Buffer.from(\"exports.handler = async (event) => { return { statusCode: 200 }; };\", \"utf8\"),\n  },\n});\n\n// Create the Lambda function\nlambdaClient.send(createFunctionCommand).then((data) => {\n  console.log(data);\n}).catch((err) => {\n  console.log(err);\n});\n```\n\nTo get started with AI agents, you need to install the required SDK and create a new Lambda function.\n\nThe 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.\n\nHere's an example of how the plan-act-observe loop works in the context of code reviews:\n\n```\n// Define the planning stage\nfunction planCodeReview(code) {\n  // Determine what actions to take based on the code\n  const actions = [];\n  if (code.includes(\"console.log\")) {\n    actions.push(\"remove console.log statements\");\n  }\n  return actions;\n}\n\n// Define the acting stage\nfunction actOnCodeReview(actions, code) {\n  // Take the planned actions and execute them\n  let updatedCode = code;\n  for (const action of actions) {\n    if (action === \"remove console.log statements\") {\n      updatedCode = updatedCode.replace(/console.log\\(.*\\);/g, \"\");\n    }\n  }\n  return updatedCode;\n}\n\n// Define the observing stage\nfunction observeCodeReview(updatedCode) {\n  // Observe the result of the action and update the plan\n  const observations = [];\n  if (updatedCode.includes(\"error\")) {\n    observations.push(\"fix error\");\n  }\n  return observations;\n}\n\n// Create a new code review agent\nfunction codeReviewAgent(code) {\n  const actions = planCodeReview(code);\n  const updatedCode = actOnCodeReview(actions, code);\n  const observations = observeCodeReview(updatedCode);\n  return observations;\n}\n```\n\nThe plan-act-observe loop is a fundamental concept in AI agents that consists of planning, acting, and observing stages.\n\nTo 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.\n\nHere's an example of how to build a simple AI agent using the `@aws-sdk/lambda`\n\npackage:\n\n``` js\n// Import the required package\nimport { LambdaClient, UpdateFunctionCodeCommand } from \"@aws-sdk/client-lambda\";\n\n// Create a new Lambda client\nconst lambdaClient = new LambdaClient({ region: \"us-east-1\" });\n\n// Define the AI agent's goals and environment\nfunction aiAgent(code) {\n  // Determine what actions to take based on the code\n  const actions = [];\n  if (code.includes(\"console.log\")) {\n    actions.push(\"remove console.log statements\");\n  }\n\n  // Take the planned actions and execute them\n  let updatedCode = code;\n  for (const action of actions) {\n    if (action === \"remove console.log statements\") {\n      updatedCode = updatedCode.replace(/console.log\\(.*\\);/g, \"\");\n    }\n  }\n\n  // Observe the result of the action and update the plan\n  const observations = [];\n  if (updatedCode.includes(\"error\")) {\n    observations.push(\"fix error\");\n  }\n\n  // Update the Lambda function code\n  const updateFunctionCodeCommand = new UpdateFunctionCodeCommand({\n    FunctionName: \"ai-agent\",\n    ZipFile: Buffer.from(updatedCode, \"utf8\"),\n  });\n\n  // Update the Lambda function\n  lambdaClient.send(updateFunctionCodeCommand).then((data) => {\n    console.log(data);\n  }).catch((err) => {\n    console.log(err);\n  });\n\n  return observations;\n}\n```\n\nTo 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.\n\nAI 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.\n\nHere's an example of how to use an AI agent to automate code reviews:\n\n``` js\n// Import the required package\nimport { LambdaClient, InvokeCommand } from \"@aws-sdk/client-lambda\";\n\n// Create a new Lambda client\nconst lambdaClient = new LambdaClient({ region: \"us-east-1\" });\n\n// Define the AI agent's goals and environment\nfunction codeReviewAgent(code) {\n  // Determine what actions to take based on the code\n  const actions = [];\n  if (code.includes(\"console.log\")) {\n    actions.push(\"remove console.log statements\");\n  }\n\n  // Take the planned actions and execute them\n  let updatedCode = code;\n  for (const action of actions) {\n    if (action === \"remove console.log statements\") {\n      updatedCode = updatedCode.replace(/console.log\\(.*\\);/g, \"\");\n    }\n  }\n\n  // Observe the result of the action and update the plan\n  const observations = [];\n  if (updatedCode.includes(\"error\")) {\n    observations.push(\"fix error\");\n  }\n\n  // Invoke the Lambda function\n  const invokeCommand = new InvokeCommand({\n    FunctionName: \"ai-agent\",\n    Payload: updatedCode,\n  });\n\n  // Invoke the Lambda function\n  lambdaClient.send(invokeCommand).then((data) => {\n    console.log(data);\n  }).catch((err) => {\n    console.log(err);\n  });\n\n  return observations;\n}\n```\n\nAI agents can be used to automate code reviews, debugging, and optimization, making them a valuable tool for software development.\n\nThe 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.\n\nHowever, 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.\n\nHere's an example of how to handle errors and exceptions in AI agent workflows:\n\n``` js\n// Import the required package\nimport { LambdaClient, InvokeCommand } from \"@aws-sdk/client-lambda\";\n\n// Create a new Lambda client\nconst lambdaClient = new LambdaClient({ region: \"us-east-1\" });\n\n// Define the AI agent's goals and environment\nfunction aiAgent(code) {\n  try {\n    // Determine what actions to take based on the code\n    const actions = [];\n    if (code.includes(\"console.log\")) {\n      actions.push(\"remove console.log statements\");\n    }\n\n    // Take the planned actions and execute them\n    let updatedCode = code;\n    for (const action of actions) {\n      if (action === \"remove console.log statements\") {\n        updatedCode = updatedCode.replace(/console.log\\(.*\\);/g, \"\");\n      }\n    }\n\n    // Observe the result of the action and update the plan\n    const observations = [];\n    if (updatedCode.includes(\"error\")) {\n      observations.push(\"fix error\");\n    }\n\n    // Invoke the Lambda function\n    const invokeCommand = new InvokeCommand({\n      FunctionName: \"ai-agent\",\n      Payload: updatedCode,\n    });\n\n    // Invoke the Lambda function\n    lambdaClient.send(invokeCommand).then((data) => {\n      console.log(data);\n    }).catch((err) => {\n      console.log(err);\n    });\n  } catch (err) {\n    console.log(err);\n  }\n}\n```\n\nTo handle errors and exceptions in AI agent workflows, developers need to implement error handling mechanisms and monitor AI agent performance.\n\nHere are the key takeaways from this post:\n\nTransparency noticeThis article was written with the help of an AI system —\n\n[Groq](LLaMA 3.3 70B).\n\nThe topic was scouted from live AWS and Node.js ecosystem signals, and the content —\n\nincluding all code examples — was written autonomously without human editing.\n\nPublished:2026-08-04 ·Primary focus:AIAgentsAll code blocks are intended to be correct and runnable, but please verify them\n\nagainst the official[AWS SDK v3 docs]\n\nbefore using in production.\n\nFind an error? Drop a comment — corrections are always welcome.", "url": "https://wpnews.pro/news/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step", "canonical_source": "https://dev.to/dineshgowtham/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step-2a9k", "published_at": "2026-08-04 13:15:38+00:00", "updated_at": "2026-08-04 13:48:59.301508+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "developer-tools"], "entities": ["AWS Lambda", "@aws-sdk/lambda"], "alternates": {"html": "https://wpnews.pro/news/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step", "markdown": "https://wpnews.pro/news/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step.md", "text": "https://wpnews.pro/news/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step.txt", "jsonld": "https://wpnews.pro/news/how-ai-agents-work-the-plan-act-observe-loop-explained-step-by-step.jsonld"}}