What is an Agent

Chatbot answers. Workflow follows. Agent decides. The autonomy spectrum — and the honest guide to when NOT to use agents.

▶ Watch this reel

What you'll learn

  1. The autonomy spectrum
  2. The agent loop
  3. Anatomy of an agent
  4. When NOT to use agents

Remember this

Autonomy spectrum

The loop (ReAct)

Anatomy

When NOT to use agents

Code: The agent loop in ~30 lines

from dataclasses import dataclass

@dataclass
class Agent:
    model: LLM
    instructions: str
    tools: dict[str, callable]      # name -> function
    max_iters: int = 10             # the budget

    def run(self, goal: str) -> str:
        messages = [
            {"role": "system", "content": self.instructions},
            {"role": "user", "content": goal},
        ]
        for _ in range(self.max_iters):        # hard stop
            resp = self.model.chat(
                messages, tools=schema(self.tools))
            msg = resp.choices[0].message
            messages.append(msg)

            if not msg.tool_calls:               # done: final answer
                return msg.content

            for call in msg.tool_calls:          # act + observe
                try:
                    result = self.tools[call.name](**call.args)
                except Exception as e:
                    result = f"ERROR: {e}"      # errors go back as observations
                messages.append({"role": "tool",
                                 "content": str(result)})
        return "Stopped: iteration budget exhausted."