Skip to main content
Call Anthropic’s Claude API directly from SQL to generate sophisticated text responses, analyze content, and perform complex reasoning tasks using state-of-the-art language models.

What is Anthropic Claude?

Claude is Anthropic’s family of large language models known for sophisticated reasoning, nuanced analysis, and helpful, harmless, and honest responses. Claude models excel at complex tasks like analysis, summarization, creative writing, and detailed explanations.

Key features

  • Advanced reasoning: Superior performance on complex analytical tasks
  • Long context: Support for extended conversations and large documents
  • Tool use: Built-in function calling capabilities
  • Safety-focused: Designed to be helpful, harmless, and honest
  • Vision support: Analyze images with Claude 3 models

Prerequisites

To use Anthropic functions, you need:
  1. An Anthropic API key from console.anthropic.com
  2. API key configured in your database (see configuration section below)

Quick start

Generate a completion

Generate text with Claude:
SELECT ai.anthropic_generate(
    'claude-3-5-sonnet-20241022',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'Explain PostgreSQL in one sentence')
    )
)->'content'->0->>'text';

Use a system prompt

Guide Claude’s behavior with a system prompt:
SELECT ai.anthropic_generate(
    'claude-3-5-sonnet-20241022',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What is a database?')
    ),
    system_prompt => 'You are a helpful database expert. Give concise, technical answers.'
)->'content'->0->>'text';

List available models

See which Claude models you can use:
SELECT * FROM ai.anthropic_list_models();

Configuration

Store your Anthropic API key securely in the database:
-- Store API key as a secret
SELECT ai.create_secret('ANTHROPIC_API_KEY', 'your-api-key-here');

-- Use the secret by name
SELECT ai.anthropic_generate(
    'claude-3-5-sonnet-20241022',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'Hello, Claude!')
    ),
    api_key_name => 'ANTHROPIC_API_KEY'
);

Available functions

Generation

Model management

Available models

Anthropic offers several Claude model families:
  • Claude 3.5 Sonnet (claude-3-5-sonnet-20241022): Best balance of intelligence and speed
  • Claude 3 Opus (claude-3-opus-20240229): Most powerful for complex tasks
  • Claude 3 Sonnet (claude-3-sonnet-20240229): Balance of intelligence and speed
  • Claude 3 Haiku (claude-3-haiku-20240307): Fast and cost-effective

Resources