AgentLearn

What is an AI agent?

A language model proposes text. An agent system turns some of those proposals into actions, observes the result, and decides what comes next.

The loop, not the personality

Our running project is a support assistant for a fictional shop. A fixed workflow always retrieves a policy and formats an answer. An agent may decide whether it needs a policy lookup, an order lookup, or a clarification. Both can be useful. More autonomy is not automatically better: every extra decision introduces another place to fail.

How it works

Keep state explicitly: the request, observations, tool results, remaining steps, and final status. The model proposes an action; application code validates and authorizes it. After execution, append the observation and choose again. Stop on a valid answer, an escalation, a deadline, or a step limit. A stopped run is not necessarily a successful run.

A concrete example

Suppose a customer asks whether an item bought 12 days ago can be returned. The assistant retrieves the current policy, checks that the policy actually covers this item, and answers with a citation. If the item category is missing, it asks a question. It must not invent a policy merely to terminate.

Apply it to your assistant

Change maxSteps to 1. Explain why the run stops without an answer. Before running the exercise, predict the result. Afterward, explain which assumption changed and add one case where the system should refuse, ask for clarification, or escalate.

All exercise inputs and outputs are deterministic teaching examples. No language model is called. Run the same idea against a versioned dataset before making a production claim.

Key takeaway

Give the model a bounded decision loop; keep authority, validation, and stopping rules in application code.

JavaScript exercise: What is an AI agent? · code experiment

Change maxSteps to 1. Explain why the run stops without an answer.

const maxSteps = 3;
const observations = [];
let status = 'budget exhausted';
for (let step = 0; step < maxSteps; step++) {
  const action = observations.length ? 'answer' : 'lookup';
  console.log({ step: step + 1, action });
  if (action === 'answer') {
    console.log('Policy says: unopened items within 30 days. [policy-1]');
    status = 'answered'; break;
  }
  observations.push({ id: 'policy-1', windowDays: 30 });
}
console.log({ status });

Knowledge check

The assistant keeps calling the same search tool. What is the most direct safeguard?

  1. Give it a more enthusiastic personality
  2. Add a step budget and detect repeated unproductive actions
  3. Hide tool errors from the model
Answer and explanation

Add a step budget and detect repeated unproductive actions

A bounded loop limits runaway execution. Repeated-action detection can escalate sooner; suppressing errors removes information needed to recover.

Sources

Read this lesson as Markdown

Continue learning

  • What is an AI agent? — A language model proposes text. An agent system turns some of those proposals into actions, observes the result, and decides what comes next.
  • The LLM core — A model predicts tokens from a context. Your application must turn that probabilistic output into a dependable interface.
  • Context engineering — Context engineering is deciding what evidence and instructions the model gets, in what order, and within what budget.
  • Prompting for agents — A useful prompt defines a job, the available evidence, the response contract, and what to do when the evidence is insufficient.