ballerinax/ai.anthropic Ballerina library

1.5.0

Overview

Anthropic provides high-performance, safe, and reliable large language models (LLMs).

The Anthropic connector offers APIs for connecting with Anthropic LLMs, enabling the integration of advanced conversational AI and language processing capabilities into applications.

Key Features

  • Connect and interact with Anthropic Large Language Models (LLMs)
  • Support for Claude 3.5 Sonnet, Claude 3 Opus, and other Claude models
  • Efficient handling of conversational prompts and responses
  • Secure communication with API key authentication

Prerequisites

Before using this module in your Ballerina application, first you must obtain the nessary configuration to engage the LLM.

Quickstart

To use the ai.anthropic module in your Ballerina application, update the .bal file as follows:

Step 1: Import the module

Import the ai.anthropic; module.

Copy
import ballerinax/ai.anthropic;

Step 2: Intialize the Model Provider

Here's how to initialize the Model Provider:

Copy
import ballerina/ai;
import ballerinax/ai.anthropic;

final ai:ModelProvider anthropicModel = check new anthropic:ModelProvider("anthropicAiApiKey", anthropic:CLAUDE_SONNET_4_6);

The first argument, apiKey, sets how the connector authenticates with the Anthropic API. It keeps its name so that existing code still compiles, but it accepts any of the following:

  • API key (string): sent in the x-api-key header, as in the example above.

  • API key with a workspace (anthropic:ApiKeyConfig): sent in the Authorization: Bearer header. Set workspaceId when the key is a personal or service account key that is not scoped to a workspace.

    Copy
    final ai:ModelProvider anthropicModel = check new anthropic:ModelProvider(
        {apiKey: "anthropicAiApiKey", workspaceId: "wrkspc_..."}, anthropic:CLAUDE_SONNET_4_6);
  • Workload Identity Federation (anthropic:WorkloadIdentityConfig): exchanges an identity token from your identity provider (for example, AWS, Google Cloud, Microsoft Entra ID, Kubernetes, or GitHub Actions) for a short-lived Anthropic access token, so no API key is needed. The connector reuses the access token until it expires. An identity token can usually be exchanged only once, so after that, create a new model provider with a new identity token. See Workload Identity Federation for how to set up the federation rule and service account.

    Copy
    final ai:ModelProvider anthropicModel = check new anthropic:ModelProvider({
        federationRuleId: "fdrl_...",
        organizationId: "00000000-0000-0000-0000-000000000000",
        identityToken: "<identity token (JWT) from your identity provider>"
    }, anthropic:CLAUDE_SONNET_4_6);
  • Access token (http:BearerTokenConfig): an access token obtained elsewhere, sent in the Authorization: Bearer header. The connector does not refresh it, so requests fail once it expires.

    Copy
    final ai:ModelProvider anthropicModel = check new anthropic:ModelProvider({token: "sk-ant-oat01-..."}, anthropic:CLAUDE_SONNET_4_6);

Step 4: Invoke chat completion

Copy
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}];
ai:ChatAssistantMessage response = check anthropicModel->chat(chatMessages, tools = []);

chatMessages.push(response);

Import

import ballerinax/ai.anthropic;Copy

Other versions

See more...

Metadata

Released date: about 7 hours ago

Version: 1.5.0

License: Apache-2.0


Security

Direct vulnerabilities: No known direct vulnerabilities

Indirect vulnerabilities: 

Checked on 2026-11-23 via OSV.dev


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 33213

Current verison: 4


Weekly downloads


Source repository


Keywords

Agent

Anthropic

Model

Provider

Vendor/Anthropic

Area/AI & Machine Learning

Type/Model Provider

Type/Connector

Name/Anthropic Model Provider


Contributors