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.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-7",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## Streaming4647```ruby48stream = client.messages.stream(49model: :"claude-opus-4-7",50max_tokens: 64000,51messages: [{ role: "user", content: "Write a haiku" }]52)5354stream.text.each { |text| print(text) }55```5657---5859## Tool Use6061The Ruby SDK supports tool use via raw JSON schema definitions and also provides a beta tool runner for automatic tool execution.6263### Tool Runner (Beta)6465```ruby66class GetWeatherInput < Anthropic::BaseModel67required :location, String, doc: "City and state, e.g. San Francisco, CA"68end6970class GetWeather < Anthropic::BaseTool71doc "Get the current weather for a location"7273input_schema GetWeatherInput7475def call(input)76"The weather in #{input.location} is sunny and 72°F."77end78end7980client.beta.messages.tool_runner(81model: :"claude-opus-4-7",82max_tokens: 16000,83tools: [GetWeather.new],84messages: [{ role: "user", content: "What's the weather in San Francisco?" }]85).each_message do |message|86puts message.content87end88```8990### Manual Loop9192See the [shared tool use concepts](../shared/tool-use-concepts.md) for the tool definition format and agentic loop pattern.9394---9596## Prompt Caching9798`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`.99100```ruby101message = client.messages.create(102model: :"claude-opus-4-7",103max_tokens: 16000,104system_: [105{ type: "text", text: long_system_prompt, cache_control: { type: "ephemeral" } }106],107messages: [{ role: "user", content: "Summarize the key points" }]108)109```110111For 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.112113Verify hits via `message.usage.cache_creation_input_tokens` / `message.usage.cache_read_input_tokens`.114