<p>Anthropic の Claude は長文理解・コード生成・日本語能力の高さが特徴の LLM です。<br>
Rails から Claude を使うには <strong>anthropic-sdk-ruby</strong>(旧 <code>anthropic</code> gem)を使います。<br>
本記事では基本的な呼び出しからストリーミング・ツール使用・Vision まで解説します。</p>

<h3 id="install">インストール</h3>
<pre><code># Gemfile
gem 'anthropic'</code></pre>
<pre><code>bundle install</code></pre>
<p><code>config/initializers/anthropic.rb</code> に API キーを設定します。</p>
<pre><code>Anthropic.configure do |config|
config.api_key = ENV['ANTHROPIC_API_KEY']
end</code></pre>

<h3 id="basic-request">基本的な呼び出し</h3>
<pre><code>client = Anthropic::Client.new

response = client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Rubyの魅力を3つ教えてください' }
]
)
puts response['content'].first['text']</code></pre>

<h3 id="system-prompt">システムプロンプトの設定</h3>
<p>Claude はシステムプロンプトを <code>system</code> パラメーターで渡します。</p>
<pre><code>response = client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 2048,
system: 'あなたは経験豊富なRailsエンジニアです。初心者にわかりやすく説明してください。',
messages: [
{ role: 'user', content: 'Active Recordとは何ですか?' }
]
)</code></pre>

<h3 id="streaming">ストリーミング</h3>
<pre><code>client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 2048,
stream: true,
messages: [{ role: 'user', content: '詳しく説明してください' }]
) do |event|
if event['type'] == 'content_block_delta'
print event.dig('delta', 'text')
end
end</code></pre>

<h3 id="tool-use">ツール使用(Function Calling)</h3>
<p>Claude に外部ツールを呼ばせる実装です。</p>
<pre><code>response = client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 1024,
tools: [{
name: 'get_stock_price',
description: '指定した銘柄の株価を取得する',
input_schema: {
type: 'object',
properties: {
ticker: { type: 'string', description: '銘柄コード(例: AAPL)' }
},
required: ['ticker']
}
}],
messages: [{ role: 'user', content: 'Appleの株価を教えて' }]
)

# ツール呼び出しが返ってきた場合
if response['stop_reason'] == 'tool_use'
tool_call = response['content'].find { |c| c['type'] == 'tool_use' }
puts "ツール: #{tool_call['name']}, 入力: #{tool_call['input']}"
end</code></pre>

<h3 id="vision">画像理解(Vision)</h3>
<p>Claude は画像を Base64 で渡すことで内容を解析できます。</p>
<pre><code>image_data = Base64.strict_encode64(File.read('screenshot.png'))

response = client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 1024,
messages: [{
role: 'user',
content: [
{
type: 'image',
source: { type: 'base64', media_type: 'image/png', data: image_data }
},
{ type: 'text', text: 'この画像に何が写っていますか?' }
]
}]
)
puts response['content'].first['text']</code></pre>

<h3 id="prompt-caching">プロンプトキャッシュ(コスト削減)</h3>
<p>長いシステムプロンプトは <code>cache_control</code> でキャッシュするとコストが最大90%削減できます。</p>
<pre><code>response = client.messages(
model: 'claude-sonnet-4-6',
max_tokens: 1024,
system: [
{
type: 'text',
text: '(長い仕様書やドキュメントをここに置く)',
cache_control: { type: 'ephemeral' }
}
],
messages: [{ role: 'user', content: '仕様書についての質問' }]
)</code></pre>

<h3 id="summary">まとめ</h3>
<p>anthropic-sdk-ruby は薄いラッパーで Anthropic の公式ドキュメントと対応が取りやすく、Claude の機能をフルに活用できます。<br>
長文処理・日本語・Vision・ツール使用を組み合わせると、Rails アプリに強力な AI 機能を追加できます。</p>

<h3 id="related">関連記事</h3>
<ul>
<li><a href="/blog/javascript/rubyllm-rails-intro">RubyLLM gem 入門|Rails に ChatGPT・Claude を1時間で組み込む方法</a></li>
<li><a href="/blog/javascript/ruby-openai-chatbot">ruby-openai で Rails チャットボットを作る|ストリーミング対応の実装入門</a></li>
</ul>