
Claude Agent SDK for Ruby

A Ruby SDK for the Claude Code agent runtime, built for running agents in production Ruby and Rails apps. It has the same capabilities as the official TypeScript and Python SDKs, plus what a Rails deploy needs around them: a generator and CLI-vendoring rake task, callbacks that are safe to touch ActiveRecord from, a pinned CLI binary, built-in OpenTelemetry tracing, and transcript mirroring to your own storage.
Unofficial and community-maintained. This project is not affiliated with or supported by Anthropic. It tracks the official SDKs release by release; see the CHANGELOG for the currently synced version.
Highlights
- Rails integration.
bin/rails generate claude_agent_sdk:install writes the initializer and bin/rails claude_agent_sdk:install_cli vendors the CLI; docs/rails.md covers jobs, ActionCable streaming, session resumption, and solid_queue fiber workers (callback_scheduling: :inline).
- Callbacks that are safe around ActiveRecord. Tool handlers, hooks, permission callbacks, and message blocks run on a plain thread by default, outside the SDK's fiber scheduler, so thread-keyed libraries (ActiveRecord,
pg, per-thread caches) behave as they do everywhere else in your app. ClaudeAgentSDK::Railtie.callback_wrapper runs them in the Rails executor so connections go back to the pool, without deadlocking development code reloading.
- Hermetic deploys.
CLIInstaller vendors a checksum-verified CLI binary, pinned to the version each gem release is tested with, so production never depends on a global npm install.
- Built-in OpenTelemetry observer with Langfuse support; no third-party instrumentation library required.
- Transcript mirroring. A
SessionStore adapter mirrors session transcripts to your own storage (reference adapters for Postgres, Redis, and S3, plus a conformance suite), and sessions can be resumed from it on another host.
- Same wire protocol as the official SDKs. Spawns the
claude CLI as a subprocess and speaks stream-JSON over stdin/stdout, so every feature of the runtime is available: sessions, subagents, sandboxing, structured output, file checkpointing and rewind.
query() for one-shot calls, Client for bidirectional sessions with interrupts, mid-session model switching, and streaming input from any Enumerator.
- In-process custom tools. Define tools as Ruby blocks; they run inside your process with direct access to your app state (SDK MCP servers), with JSON-Schema-validated arguments.
- All 33 hook events and permission callbacks with typed inputs, so you can gate, audit, or rewrite every tool call.
- Pluggable transport to run the CLI somewhere else (an E2B microVM, a container, over SSH).
Installation
# Gemfile
gem 'claude-agent-sdk', '~> 1.0'
Then bundle install, or install directly with gem install claude-agent-sdk. To track unreleased changes, point the Gemfile at GitHub: gem 'claude-agent-sdk', github: 'rubycatco/claude-agent-sdk-ruby'.
Upgrading from 0.x? 1.0 raises on unknown keys, limits #[] to attributes and adds SessionStoreError. UPGRADING-1.0.md has the checklist.
Prerequisites
- Ruby 3.2 or newer
- Credentials for Claude Code:
ANTHROPIC_API_KEY, CLAUDE_CODE_OAUTH_TOKEN, or a login the CLI has already stored (see Authentication)
- Claude Code CLI 2.0.0 or newer, either installed globally (
npm install -g @anthropic-ai/claude-code) or vendored with CLIInstaller:
# bin/setup or a cached Docker layer: the CLI version this gem release was tested with
ClaudeAgentSDK::CLIInstaller.install_pinned # => "/app/vendor/claude/claude"