NAME

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
my $response = $claude->message("What is the capital of France?");
print $response;  # prints response text

# Chat with history
my $response = $claude->chat(
    messages => [
        { role => 'user', content => 'Hello!' },
        { role => 'assistant', content => 'Hello! How can I help you today?' },
        { role => 'user', content => 'What is 2+2?' },
    ],
);

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

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

DESCRIPTION

AI::Anthropic provides a Perl interface to Anthropic's Claude API. It supports all Claude models including Claude 4 Opus, Claude 4 Sonnet, and Claude Haiku.

METHODS

AUTHOR

Vugar Bakhshaliyev <d7951500@gmail.com>

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.

new

my $claude = AI::Anthropic->new(
    api_key     => 'your-api-key',      # required (or use ANTHROPIC_API_KEY env)
    model       => 'claude-sonnet-4-20250514',  # optional
    max_tokens  => 4096,                 # optional
    timeout     => 120,                  # optional, seconds
);

message

Simple interface for single message:

my $response = $claude->message("Your question here");
my $response = $claude->message("Your question", system => "You are helpful");

print $response->text;

chat

Full chat interface:

my $response = $claude->chat(
    messages    => \@messages,       # required
    system      => $system_prompt,   # optional
    model       => $model,           # optional, overrides default
    max_tokens  => $max_tokens,      # optional
    temperature => 0.7,              # optional, 0.0-1.0
    stream      => \&callback,       # optional, for streaming
    tools       => \@tools,          # optional, for function calling
);

models

Returns list of available models:

my @models = $claude->models;

EXAMPLES

Basic usage

use AI::Anthropic;

my $claude = AI::Anthropic->new;
print $claude->message("Hello, Claude!");

With image (vision)

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

Tool use (function calling)

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

Streaming

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

ENVIRONMENT

ANTHROPIC_API_KEY

Your Anthropic API key. Can be set instead of passing api_key to new().

SEE ALSO

https://docs.anthropic.com/ - Anthropic API documentation

OpenAI::API - Similar module for OpenAI

AUTHOR

Your Name <your@email.com>

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.