#21168: feat(agents): Claude Agent SDK as alternative agent runtime
docs
agents
size: XL
Cluster:
Session Management and Fixes
## Summary
Add the [Claude Agent SDK](https://github.com/anthropics/claude-agent-sdk-typescript) as an alternative agent runtime behind a provider-agnostic abstraction layer.
- **Runtime abstraction layer** — `AgentRuntime` / `RuntimeSession` / `SessionStore` interfaces with a pi-agent reference implementation, so future runtimes slot in cleanly
- **Claude SDK runtime** — spawns Claude Code CLI as a subprocess via the SDK's `query()` function, with streaming, session resume, and abort support
- **Opt-in activation** — set `agents.defaults.runtime: "claude-sdk"` in config or `OPENCLAW_AGENT_RUNTIME=claude-sdk` env var; default remains `pi-agent` with zero impact on existing users
### Architecture
The SDK runtime uses a subprocess model (not in-process). Custom tools are provided via an MCP server, and the SDK subprocess handles its own context management, compaction, and tool execution. Events are bridged back to the existing agent event system so the gateway/TUI work unchanged.
### Key files
| File | Purpose |
|------|---------|
| `src/agents/runtime/types.ts` | Core interfaces |
| `src/agents/runtime/pi-runtime.ts` | pi-agent implementation |
| `src/agents/runtime/claude-sdk-runtime.ts` | Claude SDK implementation |
| `src/agents/runtime/claude-session-store.ts` | Session persistence for SDK |
| `src/agents/runtime/event-bridge.ts` | Event mapping |
| `src/agents/runtime/tool-bridge.ts` | Tool mapping |
| `src/agents/runtime/index.ts` | Factory + `resolveRuntimeType()` |
| `src/agents/pi-embedded-runner/run/claude-sdk-attempt.ts` | SDK attempt runner |
| `src/agents/pi-embedded-runner/run.ts` | Runtime dispatch |
| `docs/claude-sdk-runtime.md` | Setup guide |
## Testing
- CI passes (types, lint, formatting)
- Default runtime (`pi-agent`) behavior unchanged — no regressions
- SDK runtime activates when `OPENCLAW_AGENT_RUNTIME=claude-sdk` is set
- Lightly tested end-to-end with local OpenClaw instance
## AI disclosure
This PR was AI-assisted (Claude Opus 4.6 via Claude Code). The contributor understands all code changes and has tested them locally.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Most Similar PRs
#19136: feat(claude-code): implement spawn mode for Claude Code sub-agents
by botverse · 2026-02-17
71.2%
#13167: feat(agents): dispatch Claude Code CLI runs as persistent, resumabl...
by gyant · 2026-02-10
69.6%
#23700: feat: Claude CLI personal-use auth (no API key required) + native A...
by 88plug · 2026-02-22
67.8%
#13407: feat: add Anthropic Vertex AI provider extension
by roccolangeweg · 2026-02-10
66.0%
#21035: feat: agent hardening — modifying after_tool_call hook, cron contex...
by roelven · 2026-02-19
65.6%
#9049: fix: prevent subagent stuck loops and ensure user feedback
by maxtongwang · 2026-02-04
65.4%
#2806: [AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude o...
by Arthur742Ramos · 2026-01-27
64.6%
#18670: feat: add first-class Claude Code CLI auth path + CLI model UX hard...
by SmithLabsLLC · 2026-02-16
64.4%
#16099: feat: add opencode-cli as CLI backend provider
by imwxc · 2026-02-14
64.1%
#15379: fix(antigravity): strip unsigned thinking blocks in agent loop via ...
by jg-noncelogic · 2026-02-13
62.7%