Get started with Spring AI and Anthropic

Neven CvetkovicNeven Cvetkovic
4 min read

Introduction

In this article we look at how to get started with Spring AI project and connect to Anthropic Large Language Models (LLMs).

Feel free to browse other articles in this getting started with Spring AI series:

Create a simple Spring Application (Beginners)

We can use Spring Initializr to generate a skeleton project for our simple application. You can do that either in IDE itself, or directly with a pre-configured project (e.g. Maven, Java, Spring Boot 3.4.4, spring-ai-demo, com.example package name. JAR packaging, Java 24 with Spring Web).

Clicking on “Generate” button will download a project skeleton zip archive, e.g. spring-ai-demo.zip which you can unzip and open in editor of your choice (e.g. IntelliJ, VS Code, etc.)

We can start adding specific AI model Spring Boot starters later on.

Let’s add a HomeController, e.g.

package com.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "Hello World!";
    }
}

Let’s run our simple application, e.g.

./mvnw spring-boot:run

Feel free to browse from another terminal window, e.g. http localhost:8080

http localhost:8080

HTTP/1.1 200 
Connection: keep-alive
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:00:00 GMT
Keep-Alive: timeout=60

Hello World!

or a directly in the browser at http://localhost:8080

Feel free to explore Spring Boot Getting Started Guide for more details on how to create a simple Spring Boot web application.

Connecting your Spring Application to Anthropic models

Configure Anthropic

In this section we will explore connecting your application to Anthropic Large Language Models (LLMs). If you don’t already have an account on Anthropic API platform, please sign up for an account, e.g.

Once you have created and authenticated your account, you can create a new API Key,

Please save the key (e.g. sk-ant-api03-IywAWAvco...), as you won’t be able to view it again. Keep it secure, because anyone with your API key can access and consume your Anthropic API platform credits.

NOTE: Of course, I regenerated my API key after taking the screenshots. 🤣

Finally, let’s add Spring AI code to start using Anthropic API.

Add Spring AI for Anthropic

We will add Spring AI Spring Boot Starter for Anthropic in our Maven pom.xml, e.g.

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-anthropic</artifactId>
            <version>1.0.0-M7</version>
        </dependency>

Notice, we are using version 1.0.0-M7 that’s newest at the time of writing this article. Please replace with the latest Spring AI version.

We also need to add few Spring properties to our src/main/resources/application.properties file, e.g.

spring.application.name=spring-ai-demo
spring.ai.anthropic.api-key=sk-ant-api03-IywAWAvco...
spring.ai.anthropic.chat.options.model=claude-3-5-haiku-latest

/quoteNotice, we have used claude-3-5-haiku-latest large language model (LLM) for this simple use case. Feel free to explore all Antropic LLM models. These models vary in size, speed, training data and eventually in price per 1M tokens. For example, claude-3-5-haiku-latest is the fastest model and costs a third of the price of more powerful claude-3-7-sonnet-latest model. The claude-3-5-haiku-latest costs per 1M tokens (MTOK) are: (1) for input US$0.80 and (2) for output US$4.00, whereas claude-3-7-sonnet-latest costs are (1) for input $3.00 and (2) for output $15.00 per 1M tokens.

Finally, let’s add a call to a LLM from our HomeController, e.g.

package com.example;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    private ChatClient chatClient;

    public HomeController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping("/")
    public String home() {
        return chatClient
                .prompt()
                .user("who are you")
                .call()
                .content();
    } 
}

Run the application again, e.g.

./mvnw spring-boot:run

Test the application, in another terminal or browser, e.g.

http localhost:8080

HTTP/1.1 200 
Connection: keep-alive
Content-Length: 266
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:05:00 GMT
Keep-Alive: timeout=60

I'm Claude, an AI created by Anthropic. I aim to be helpful, honest, and
harmless. I won't pretend to be human, and I'm always direct about being
an AI. I'm happy to help you with tasks or have a conversation, while being
clear about my capabilities and limitations.

🏆 Congratulations! You have created your first application that talks to an Anthropic LLM service.

Possible Gotcha’s

If you run into Incorrect API key provided (see below example) - this is probably because you have no funds available in your Anthropic account, or you mistyped the Anthropic API key.

org.springframework.ai.retry.NonTransientAiException: 401 - {
    "type" : "error",
    "error": {
        "type":"authentication_error",
        "message":"invalid x-api-key"
    }
}

Check that you have some funds available in your account, e.g.

You might need to set the billing and add $5 to get started, e.g.

Hope you enjoyed this beginners getting started with Spring AI article!

Connecting to other LLMs

If you want to run your Spring AI application with other LLM models, look at our articles:

References

0
Subscribe to my newsletter

Read articles from Neven Cvetkovic directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Neven Cvetkovic
Neven Cvetkovic