# How AI Agents Work: Connectors, MCP & Agent Safety | Stanford CS193T Lecture 5 with Cynthia Bailey

## Executive summary

This lecture details the architecture of AI agents, moving beyond simple generative content creation to autonomous action execution. Agents utilize 'connectors'—access permissions to external services (e.g., Gmail, Outlook)—which allow the Large Language Model (LLM) to perform actions by generating highly structured JSON text. The core mechanism remains next-token prediction, but the output is interpreted by a 'connector manager' to trigger tool calls. Effective agent design requires defining a detailed scope of work, including goals, context, tools, permissions, memory, actions, and crucial stopping conditions. Safety is paramount, requiring the application of principles like 'least privilege access' and careful mitigation of prompt injection risks.

## Key takeaways

- Agentic AI vs. Generative AI: Generative AI focuses on content creation, while agentic AI focuses on taking actions. However, technically, both rely on the LLM's underlying next-token prediction mechanism. When acting, the LLM emits structured JSON text that a connector module intercepts and executes as a tool call.
- The Anatomy of an Agent: A well-specified agent must define seven parts: goal, context, tools, permissions, memory, actions, and stopping conditions. Stopping conditions are critical, as agents will continue running until explicitly told to stop or until a predefined condition is met.
- The Analyze-Act-Observe Loop: Agent workflows operate in a continuous loop: the LLM analyzes the context to identify necessary actions, the action is executed via a connector, and the result is observed and fed back into the context for the next analysis cycle. The distinction from traditional AI is the potential for this loop to continue many times.
- The Elephant and the Goldfish Method: This professional-grade workflow strategy uses two agents: the 'Elephant' (with long memory) to generate a comprehensive, detailed scope of work, and the 'Goldfish' (with no memory) to assess if the document is complete enough for autonomous execution. The process repeats until the Goldfish confirms no clarifying questions remain.

## Technical details

- Connectors and Model Context Protocol (MCP): Connectors are access permissions that plug AI tools into external apps (e.g., Gmail, Outlook). Enabling a connector injects specific instructions and tool definitions (in JSON schema format) into the LLM's context window. The resulting structured text output is governed by the Model Context Protocol (MCP).
- LLM Mechanism for Tool Calling: The LLM does not change its core function; it still performs next-token prediction. When acting, the LLM generates highly structured text (like software code) that functions as a call to a tool. A 'connector manager' reads this structured text, recognizes it, and dispatches the actual call to the enabled connector.
- Security and Risk Mitigation: Key security principles include 'least privilege access' (only granting necessary permissions) and rigorous attention to 'prompt injection' risks. The risk of an agent having overly broad permissions (e.g., permanent file deletion) must be carefully assessed.

## Practical implications

- When designing agents, treat the agent specification as a detailed scope of work, ensuring all seven components (Goal, Context, Tools, Permissions, Memory, Actions, Stopping Conditions) are explicitly defined.
- Implement human-in-the-loop checkpoints, especially when the agent's actions are irreversible or highly impactful, to maintain human oversight.
- For critical, complex tasks, use structured methods like the 'Elephant and the Goldfish' process to iteratively refine the scope of work and minimize ambiguity before deployment.

## Topics

AI Agents, Agentic Workflow, LLM Architecture, System Design, AI Safety and Governance, Stanford Online AI Courses

Source: https://www.youtube.com/watch?v=ho00IesajkY
