AI::Anthropic

Perl interface to Anthropic's Claude API.

Synopsis

use AI::Anthropic;

my $claude = AI::Anthropic->new(
    api_key => 'sk-ant-api03-your-key-here',
);

# Simple message
print $claude->message("What is the meaning of life?");

# With system prompt
my $response = $claude->chat(
    system   => 'You are a helpful Perl programmer.',
    messages => [
        { role => 'user', content => 'How do I read a file?' },
    ],
);

print "Response: ", $response->text, "\n";
print "Tokens: ", $response->total_tokens, "\n";

Installation

From CPAN:

cpanm AI::Anthropic

Or manually:

perl Makefile.PL
make
make test
make install

Features

Quick Start

use AI::Anthropic;

my $claude = AI::Anthropic->new(
    api_key => 'sk-ant-api03-your-key-here',
);

print $claude->message("Hello!");

Streaming

$claude->chat(
    messages => [ { role => 'user', content => 'Tell me a story' } ],
    stream   => sub {
        my ($chunk) = @_;
        print $chunk;
        STDOUT->flush;
    },
);

Vision (Images)

# From file
my $response = $claude->chat(
    messages => [
        {
            role    => 'user',
            content => [
                { type => 'text', text => 'What is in this image?' },
                { type => 'image', path => '/path/to/image.jpg' },
            ],
        },
    ],
);

# From URL
my $response = $claude->chat(
    messages => [
        {
            role    => 'user',
            content => [
                { type => 'text', text => 'Describe this image' },
                { type => 'image', url => 'https://example.com/image.png' },
            ],
        },
    ],
);

Tool Use (Function Calling)

my $response = $claude->chat(
    messages => [
        { role => 'user', content => 'What is the weather in Baku?' },
    ],
    tools => [
        {
            name        => 'get_weather',
            description => 'Get current weather for a location',
            input_schema => {
                type       => 'object',
                properties => {
                    location => {
                        type        => 'string',
                        description => 'City name',
                    },
                },
                required => ['location'],
            },
        },
    ],
);

Response Object

my $response = $claude->message("Hello");

$response->text;          # Response text
$response->model;         # Model used
$response->stop_reason;   # Why generation stopped
$response->input_tokens;  # Tokens in prompt
$response->output_tokens; # Tokens in response
$response->total_tokens;  # Total tokens
$response->raw_response;  # Full API response hashref

# Stringifies to text
print "$response";

Configuration

my $claude = AI::Anthropic->new(
    api_key     => 'sk-ant-...',           # or use ANTHROPIC_API_KEY env
    model       => 'claude-opus-4-20250514', # default: claude-sonnet-4-20250514
    max_tokens  => 8192,                   # default: 4096
    timeout     => 300,                    # default: 120 seconds
);

Available Models

my @models = $claude->models;
# claude-opus-4-20250514
# claude-sonnet-4-20250514
# claude-sonnet-4-5-20250929
# claude-haiku-4-5-20251001
# claude-3-5-sonnet-20241022
# ... and more

Environment Variables

Dependencies

All dependencies are core modules or widely available on CPAN.

Why This Module?

See Also

Contributing

Pull requests welcome! Please include tests for new features.

License

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

Author

Your Name your@email.com