Leveraging Anthropic's Agent Skills with Spring AI for Document Generation

Introduction

Large language models (LLMs) have evolved far beyond simple text generation. Today, they can act as intelligent agents capable of performing complex tasks. One exciting capability is agent skills, introduced by Anthropic, which allow models to create document files directly—no extra APIs like Apache POI required. Spring AI seamlessly integrates these skills with Java applications, enabling developers to generate formatted documents on the server side with minimal effort. In this article, we'll explore how Anthropic's pre-built skills work and how to incorporate them into your Spring AI projects.

Leveraging Anthropic's Agent Skills with Spring AI for Document Generation
Source: www.baeldung.com

Understanding Anthropic Pre-built Skills

Anthropic provides a set of pre-built agent skills that enable the LLM to output rich document files instead of plain text. These skills invoke built-in capabilities on Anthropic's servers, which generate the document and return it via a file identifier. Currently, the supported formats include:

In Spring AI, you can leverage these skills by applying them through chat options, making it straightforward to request a document in a specific format directly from your Java code.

Setting Up Spring AI for Anthropic

Adding the Dependency

To get started, add the spring-ai-starter-model-anthropic dependency to your pom.xml file:

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

Configuring the API Key

Next, set your Anthropic API key in application.yml as shown below. This approach keeps the key secure and outside your source code:

spring:
  ai:
    anthropic:
      api-key: "${ANTHROPIC_API_KEY}"

With this configuration, your Spring AI application can authenticate with Anthropic's services automatically.

Practical Example: Generating a Sales Report

Let's build a simple web application that produces a sales report in a chosen document format using the pre-built skills. We'll start by creating a data model and service, then a controller to handle requests.

Data Model and Service

First, define a record to represent monthly sales data:

public record MonthlySale(String product, int year, Month month, BigDecimal amount) {}

Next, create a service that supplies sample data (e.g., 12 months of sales for two products):

@Service
public class MonthlySalesService {
    public List<MonthlySale> getMonthlySalesForYear(int year) {
        return List.of(
            new MonthlySale("Product A", year, Month.JANUARY, new BigDecimal("1200")),
            new MonthlySale("Product A", year, Month.FEBRUARY, new BigDecimal("1325")),
            // ... other months
            new MonthlySale("Product B", year, Month.NOVEMBER, new BigDecimal("1330")),
            new MonthlySale("Product B", year, Month.DECEMBER, new BigDecimal("1395"))
        );
    }
}

In a real application, this data could come from a database or an MCP tool.

Leveraging Anthropic's Agent Skills with Spring AI for Document Generation
Source: www.baeldung.com

Creating the Controller

Now implement a REST controller that accepts a year and a format (e.g., "pdf", "docx") and returns a generated document:

@RestController
public class SalesReportController {
    @Autowired
    private MonthlySalesService salesService;
    @Autowired
    private AnthropicChatModel chatModel;

    @GetMapping("/report/{year}/{format}")
    public ResponseEntity<byte[]> generateReport(@PathVariable int year, @PathVariable String format) {
        List<MonthlySale> sales = salesService.getMonthlySalesForYear(year);
        String prompt = buildPrompt(sales, format);
        // Use chat options to request the specific format
        ChatOptions options = ChatOptions.builder()
            .withSkill(Skill.DOCUMENT_GENERATION)
            .withFormat(format)
            .build();
        ChatResponse response = chatModel.call(new Prompt(prompt, options));
        byte[] document = response.getResult().getOutput().getDocument();
        return ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(document);
    }

    private String buildPrompt(List<MonthlySale> sales, String format) {
        // Convert sales data to a textual representation
        StringBuilder sb = new StringBuilder("Generate a sales report for year ");
        sb.append(sales.get(0).year()).append(" in ").append(format).append(" format.");
        sb.append("Data: ").append(sales.toString());
        return sb.toString();
    }
}

Testing the Application

Start your Spring Boot application and make a request, for example:

GET /report/2025/pdf

This will return a PDF file containing the sales report. You can change the format to "docx" or "xlsx" to get different file types. The pre-built skill handles all the formatting on the server side.

Conclusion

Anthropic's agent skills, combined with Spring AI, empower Java developers to generate rich document files from LLMs without additional libraries. By simply configuring the API key, adding a dependency, and using chat options to specify the desired skill and format, you can automate report creation and other document-heavy tasks. As LLMs continue to evolve, such integrations will become essential for building intelligent, production-ready applications.

Tags:

Recommended

Discover More

6 Ways the Criminal IP-Securonix ThreatQ Integration Revolutionizes Threat IntelligenceGreg Kroah-Hartman Releases Seven New Stable Linux Kernels with Critical Security PatchesAmazon Now Goes Live: 30-Minute Delivery Expands to Major US CitiesHow 100 Self-Driving Cars Tamed Traffic Jams Using Reinforcement Learning10 Critical Facts About Microsoft Defender's False Positive That Flagged DigiCert Certificates as Malware