Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build LLM-powered apps with the Anthropic Claude API or SDK across Python, TypeScript, Java, Go, Ruby, C#, and PHP.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
ruby/claude-api/README.md
1# Claude API — Ruby23> **Note:** The Ruby SDK supports the Claude API. A tool runner is available in beta via `client.beta.messages.tool_runner()`. Agent SDK is not yet available for Ruby.45## Installation67```bash8gem install anthropic9```1011## Client Initialization1213```ruby14require "anthropic"1516# Default (uses ANTHROPIC_API_KEY env var)17client = Anthropic::Client.new1819# Explicit API key20client = Anthropic::Client.new(api_key: "your-api-key")21```2223---2425## Basic Message Request2627```ruby28message = client.messages.create(29model: :"claude-opus-4-8",30max_tokens: 16000,31messages: [32{ role: "user", content: "What is the capital of France?" }33]34)35# content is an array of polymorphic block objects (TextBlock, ThinkingBlock,36# ToolUseBlock, ...). .type is a Symbol — compare with :text, not "text".37# .text raises NoMethodError on non-TextBlock entries.38message.content.each do |block|39puts block.text if block.type == :text40end41```4243---4445## Extended Thinking4647> **Fable 5, Opus 4.8, Opus 4.7, Opus 4.6, and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is removed on Fable 5, Opus 4.8, and 4.7 (400 if sent); deprecated on Opus 4.6 and Sonnet 4.6.48> **Older models:** Use `thinking: { type: "enabled", budget_tokens: N }` (must be < `max_tokens`, min 1024).4950```ruby51message = client.messages.create(52model: :"claude-opus-4-8",53max_tokens: 16000,54thinking: { type: "adaptive" },55messages: [{ role: "user", content: "Solve: 27 * 453" }]56)5758message.content.each do |block|59case block.type60when :thinking then puts "Thinking: #{block.thinking}"61when :text then puts "Response: #{block.text}"62end63end64```6566---6768## Prompt Caching6970`system_:` (trailing underscore — avoids shadowing `Kernel#system`) takes an array of text blocks; set `cache_control` on the last block. Plain hashes work via the `OrHash` type alias. For placement patterns and the silent-invalidator audit checklist, see `shared/prompt-caching.md`.7172```ruby73message = client.messages.create(74model: :"claude-opus-4-8",75max_tokens: 16000,76system_: [77{ type: "text", text: long_system_prompt, cache_control: { type: "ephemeral" } }78],79messages: [{ role: "user", content: "Summarize the key points" }]80)81```8283For 1-hour TTL: `cache_control: { type: "ephemeral", ttl: "1h" }`. There's also a top-level `cache_control:` on `messages.create` that auto-places on the last cacheable block.8485Verify hits via `message.usage.cache_creation_input_tokens` / `message.usage.cache_read_input_tokens`.8687---8889## Stop Details9091When `stop_reason` is `:refusal`, the response includes structured `stop_details`:9293```ruby94if message.stop_reason == :refusal && message.stop_details95puts "Category: #{message.stop_details.category}" # e.g. :cyber, :bio, :reasoning_extraction, :frontier_llm, or nil — see docs for the full set96puts "Explanation: #{message.stop_details.explanation}"97end98```99100**Refusal fallbacks (Claude Fable 5) — opt in by default.** Fallbacks are opt-in: without them a refused request simply stops. New `claude-fable-5` code should include the server-side `fallbacks` parameter (beta header `server-side-fallback-2026-06-01`, `fallbacks: [{model: "claude-opus-4-8"}]` on the beta messages call) by default. The exact Ruby binding (and the client-side middleware for providers without server-side support) is not documented here — WebFetch the Ruby SDK repo's `examples/` from `shared/live-sources.md`; full semantics in `shared/model-migration.md` → Migrating to Claude Fable 5 → `refusal` stop reason.101102---103104## Beta Features105106`betas:` is only valid on `client.beta.messages.create`, not the non-beta path.107108### Task budgets109110```ruby111response = client.beta.messages.create(112model: :"claude-opus-4-8",113max_tokens: 16000,114output_config: { task_budget: { type: :tokens, total: 64_000 } },115tools: [...],116messages: [...],117betas: ["task-budgets-2026-03-13"]118)119```120121---122123## Error Type124125`APIStatusError` exposes a `.type` field for programmatic error classification:126127```ruby128begin129client.messages.create(...)130rescue Anthropic::Errors::APIStatusError => e131puts e.type # :rate_limit_error, :overloaded_error, etc.132end133```134