Was this page helpful?
Diese Anleitung führt Sie durch das Erstellen eines Agenten, das Einrichten einer Umgebung, das Starten einer Sitzung und das Streamen von Agentenantworten.
| Concept | Description |
|---|---|
| Agent | The model, system prompt, tools, MCP servers, and skills |
| Environment | A configured container template (packages, network access) |
| Session | A running agent instance within an environment, performing a specific task and generating outputs |
| Events | Messages exchanged between your application and the agent (user turns, tool results, status updates) |
Installation überprüfen:
ant --versionSetzen Sie Ihren API-Schlüssel als Umgebungsvariable:
export ANTHROPIC_API_KEY="your-api-key-here"Alle Managed Agents API-Anfragen erfordern den managed-agents-2026-04-01 Beta-Header. Das SDK setzt den Beta-Header automatisch.
Wenn Sie ein Benutzerereignis senden, führt Claude Managed Agents folgendes aus:
session.status_idle-Ereignis, wenn er nichts mehr zu tun hatEinen Agenten erstellen
Erstellen Sie einen Agenten, der das Modell, den System-Prompt und die verfügbaren Tools definiert.
set -euo pipefail
agent=$(
curl -sS --fail-with-body https://api.anthropic.com/v1/agents \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
-d @- <<'EOF'
{
"name": "Coding Assistant",
"model": "claude-sonnet-4-6",
"system": "You are a helpful coding assistant. Write clean, well-documented code.",
"tools": [
{"type": "agent_toolset_20260401"}
]
}
EOF
)
AGENT_ID=$(jq -er '.id' <<<"$agent")
AGENT_VERSION=$(jq -er '.version' <<<"$agent")
echo "Agent ID: $AGENT_ID, version: $AGENT_VERSION"Der Tool-Typ agent_toolset_20260401 aktiviert den vollständigen Satz vorgefertigter Agenten-Tools (Bash, Dateioperationen, Websuche und mehr). Siehe Tools für die vollständige Liste und Konfigurationsoptionen pro Tool.
Speichern Sie die zurückgegebene agent.id. Sie werden sie in jeder Sitzung referenzieren, die Sie erstellen.
Eine Umgebung erstellen
Eine Umgebung definiert den Container, in dem Ihr Agent ausgeführt wird.
environment=$(
curl -sS --fail-with-body https://api.anthropic.com/v1/environments \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
-d @- <<'EOF'
{
"name": "quickstart-env",
"config": {
"type": "cloud",
"networking": {"type": "unrestricted"}
}
}
EOF
)
ENVIRONMENT_ID=$(jq -er '.id' <<<"$environment")
echo "Environment ID: $ENVIRONMENT_ID"Speichern Sie die zurückgegebene environment.id. Sie werden sie in jeder Sitzung referenzieren, die Sie erstellen.
Eine Sitzung starten
Erstellen Sie eine Sitzung, die Ihren Agenten und Ihre Umgebung referenziert.
session=$(
curl -sS --fail-with-body https://api.anthropic.com/v1/sessions \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
-d @- <<EOF
{
"agent": "$AGENT_ID",
"environment_id": "$ENVIRONMENT_ID",
"title": "Quickstart session"
}
EOF
)
SESSION_ID=$(jq -er '.id' <<<"$session")
echo "Session ID: $SESSION_ID"Eine Nachricht senden und die Antwort streamen
Öffnen Sie einen Stream, senden Sie ein Benutzerereignis und verarbeiten Sie dann Ereignisse, sobald sie eintreffen:
# Send the user message first; the API buffers events until the stream attaches
curl -sS --fail-with-body \
"https://api.anthropic.com/v1/sessions/$SESSION_ID/events" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "content-type: application/json" \
-d @- >/dev/null <<'EOF'
{
"events": [
{
"type": "user.message",
"content": [
{
"type": "text",
"text": "Create a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt"
}
]
}
]
}
EOF
# Open the SSE stream and process events as they arrive
while IFS= read -r line; do
[[ $line == data:* ]] || continue
json=${line#data: }
case $(jq -r '.type' <<<"$json") in
agent.message)
jq -j '.content[] | select(.type == "text") | .text' <<<"$json"
;;
agent.tool_use)
printf '\n[Using tool: %s]\n' "$(jq -r '.name' <<<"$json")"
;;
session.status_idle)
printf '\n\nAgent finished.\n'
break
;;
esac
done < <(
curl -sS -N --fail-with-body \
"https://api.anthropic.com/v1/sessions/$SESSION_ID/stream" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: managed-agents-2026-04-01" \
-H "Accept: text/event-stream"
)Der Agent wird ein Python-Skript schreiben, es im Container ausführen und überprüfen, ob die Ausgabedatei erstellt wurde. Ihre Ausgabe wird ähnlich wie folgt aussehen:
I'll create a Python script that generates the first 20 Fibonacci numbers and saves them to a file.
[Using tool: write]
[Using tool: bash]
The script ran successfully. Let me verify the output file.
[Using tool: bash]
fibonacci.txt contains the first 20 Fibonacci numbers (0 through 4181).
Agent finished.Netzwerk- und Container-Einstellungen anpassen