claude-code-system-prompts/system-prompts/data-claude-api-reference-ruby.md
2026-02-18 14:50:58 -07:00

88 lines
1.9 KiB
Markdown

<!--
name: 'Data: Claude API reference — Ruby'
description: Ruby SDK reference including installation, client initialization, basic requests, streaming, and beta tool runner
ccVersion: 2.1.47
-->
# Claude API — Ruby
> **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.
## Installation
\`\`\`bash
gem install anthropic
\`\`\`
## Client Initialization
\`\`\`ruby
require "anthropic"
# Default (uses ANTHROPIC_API_KEY env var)
client = Anthropic::Client.new
# Explicit API key
client = Anthropic::Client.new(api_key: "your-api-key")
\`\`\`
---
## Basic Message Request
\`\`\`ruby
message = client.messages.create(
model: :"claude-opus-4-6",
max_tokens: 1024,
messages: [
{ role: "user", content: "What is the capital of France?" }
]
)
puts message.content.first.text
\`\`\`
---
## Streaming
\`\`\`ruby
stream = client.messages.stream(
model: :"claude-opus-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a haiku" }]
)
stream.text.each { |text| print(text) }
\`\`\`
---
## Tool Use
The Ruby SDK supports tool use via raw JSON schema definitions and also provides a beta tool runner for automatic tool execution.
### Tool Runner (Beta)
\`\`\`ruby
class GetWeather < Anthropic::BaseTool
input_schema do
property :location, type: "string", description: "City and state, e.g. San Francisco, CA", required: true
end
def call(location:)
# Your tool implementation
"The weather in #{location} is sunny and 72°F."
end
end
client.beta.messages.tool_runner(
model: :"claude-opus-4-6",
max_tokens: 1024,
tools: [GetWeather.new],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }]
).each_message { |msg| puts msg.content }
\`\`\`
### Manual Loop
See the [shared tool use concepts](../shared/tool-use-concepts.md) for the tool definition format and agentic loop pattern.