|
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | | - "source": "# Getting Started with the Claude Agent API\n\nBuilding AI agents that maintain state, execute code, and interact with files typically requires managing complex infrastructure: sandboxed environments, persistent sessions, and streaming protocols. The Claude Agent API handles all of this for you, providing managed compute environments with built-in tools so you can focus on what your agent does rather than how it runs.\n\nWith the Agent API, you get persistent environments that survive across interactions, stateful sessions that remember context, and real-time streaming for responsive user experiences.\n\nBy the end of this cookbook, you'll be able to:\n\n- **Create and configure environments** with custom networking policies for secure agent execution\n- **Launch stateful agent sessions** with built-in tools (bash, file I/O, web access) and optional GitHub repo mounting\n- **Stream real-time responses** from agents, including tool use events and text output\n- **Manage the full lifecycle** of environments and sessions (list, update, archive)\n\n> **Note:** The Claude Agent API is currently in beta. API signatures and features may change." |
| 6 | + "source": "# Getting Started with the Claude Agent API\n\nBuilding AI agents that maintain state, execute code, and interact with files typically requires stitching together sandboxed environments, persistent storage, and streaming protocols yourself. The Claude Agent API handles this infrastructure so you can focus on what your agent does rather than how it runs.\n\nBy the end of this cookbook, you'll be able to:\n\n- **Create and configure environments** with custom networking policies for secure agent execution\n- **Launch stateful agent sessions** with built-in tools (bash, file I/O, web access) and optional GitHub repo mounting\n- **Stream real-time responses** from agents, including tool use events and text output\n- **Manage the full lifecycle** of environments and sessions (list, update, archive)\n\n> **Note:** The Claude Agent API is currently in beta. API signatures and features may change." |
7 | 7 | }, |
8 | 8 | { |
9 | 9 | "cell_type": "markdown", |
10 | 10 | "metadata": {}, |
11 | | - "source": [ |
12 | | - "## Table of Contents\n", |
13 | | - "\n", |
14 | | - "1. [Overview & Concepts](#overview)\n", |
15 | | - "2. [Setup & Installation](#setup)\n", |
16 | | - "3. [Creating an Environment](#environments)\n", |
17 | | - "4. [Creating Sessions](#sessions)\n", |
18 | | - "5. [Interacting with Sessions](#interacting)\n", |
19 | | - "6. [Managing Sessions](#managing)\n", |
20 | | - "7. [Best Practices](#best-practices)" |
21 | | - ] |
| 11 | + "source": "## Table of Contents\n\n1. [Overview & Concepts](#overview)\n2. [Setup & Installation](#setup)\n3. [Creating an Environment](#environments)\n4. [Creating Sessions](#sessions)\n5. [Interacting with Sessions](#interacting)\n6. [Managing Sessions & Environments](#managing)\n7. [Best Practices](#best-practices)" |
22 | 12 | }, |
23 | 13 | { |
24 | 14 | "cell_type": "markdown", |
|
67 | 57 | { |
68 | 58 | "cell_type": "markdown", |
69 | 59 | "metadata": {}, |
70 | | - "source": [ |
71 | | - "### Configure API Key\n", |
72 | | - "\n", |
73 | | - "Set your API key as an environment variable or load it from a `.env` file:" |
74 | | - ] |
| 60 | + "source": "### Configure API Key\n\nCreate a `.env` file in this directory with your API key:\n\n```\nANTHROPIC_API_KEY=sk-ant-...\n```" |
75 | 61 | }, |
76 | 62 | { |
77 | 63 | "cell_type": "code", |
78 | 64 | "execution_count": null, |
79 | 65 | "metadata": {}, |
80 | 66 | "outputs": [], |
81 | | - "source": "# Option 1: Set directly (not recommended for production)\n# os.environ[\"ANTHROPIC_API_KEY\"] = \"your-api-key-here\"\n\n# Option 2: Load from .env file\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\n# Initialize the client with beta headers\nclient = anthropic.Anthropic(\n default_headers={\n \"anthropic-beta\": \"environments-2025-12-12,agent-api-2025-12-12\",\n },\n)\n\n# Verify connection\nif os.getenv(\"ANTHROPIC_API_KEY\"):\n print(\"API key configured successfully\")\n print(f\"Base URL: {client.base_url}\")\nelse:\n print(\"Warning: ANTHROPIC_API_KEY not found in environment\")" |
| 67 | + "source": "from dotenv import load_dotenv\n\nload_dotenv()\n\n# Initialize the client with beta headers\nclient = anthropic.Anthropic(\n default_headers={\n \"anthropic-beta\": \"environments-2025-12-12,agent-api-2025-12-12\",\n },\n)\n\n# Verify connection\nif os.environ.get(\"ANTHROPIC_API_KEY\"):\n print(\"API key configured successfully\")\n print(f\"Base URL: {client.base_url}\")\nelse:\n print(\"Warning: ANTHROPIC_API_KEY not found in environment\")" |
82 | 68 | }, |
83 | 69 | { |
84 | 70 | "cell_type": "markdown", |
|
446 | 432 | { |
447 | 433 | "cell_type": "markdown", |
448 | 434 | "metadata": {}, |
449 | | - "source": [ |
450 | | - "### Viewing Session Events\n", |
451 | | - "\n", |
452 | | - "You can retrieve all events from a session to see the full conversation history:" |
453 | | - ] |
| 435 | + "source": "## 6. Managing Sessions & Environments {#managing}\n\n### Viewing Session Events\n\nYou can retrieve all events from a session to see the full conversation history:" |
454 | 436 | }, |
455 | 437 | { |
456 | 438 | "cell_type": "code", |
|
731 | 713 | "execution_count": null, |
732 | 714 | "metadata": {}, |
733 | 715 | "outputs": [], |
734 | | - "source": "# Uncomment to clean up resources:\n\n# Archive the session\n# client.beta.sessions.archive(session_id=session.id)\n# print(f\"Archived session: {session.id}\")\n\n# Archive the environment (optional - you may want to reuse it)\nclient.beta.environments.archive(environment_id=environment.id)\nprint(f\"Archived environment: {environment.id}\")" |
| 716 | + "source": "# Uncomment to clean up resources:\n\n# Archive the session\n# client.beta.sessions.archive(session_id=session.id)\n# print(f\"Archived session: {session.id}\")\n\n# Archive the environment (optional - you may want to reuse it)\n# client.beta.environments.archive(environment_id=environment.id)\n# print(f\"Archived environment: {environment.id}\")" |
735 | 717 | }, |
736 | 718 | { |
737 | 719 | "cell_type": "markdown", |
|
0 commit comments