Back to AI Tools & Agents

langchain4j-spring-boot-integration

LangChain4jSpring BootAIJavaIntegrationMicroservicesRAGDeclarative AI
282📄 MIT🕒 2026-06-15Source ↗

Install this skill

npx skills add giuseppe-trisciuoglio/developer-kit

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

The LangChain4j Spring Boot starter streamlines the integration of large language models into standard Java environments by bridging AI capabilities with the Spring container. Instead of manually managing HTTP clients for AI APIs, developers use simple interfaces marked with the @AiService annotation. The integration automatically maps Spring properties to model configuration, handles dependency injection for tools, and supports complex patterns like Retrieval Augmented Generation. By treating LLMs as standard injectable beans, it enables developers to maintain consistent application lifecycles, configuration externalization, and environment profiles. This approach allows teams to build conversational agents, autonomous tools, or document processors using familiar Spring idioms, ensuring that AI-enhanced components remain testable and consistent with the broader application architecture.

When to Use This Skill

  • Adding conversational support to existing Spring Boot microservices
  • Building RAG-based document search engines with local or cloud databases
  • Orchestrating multi-step agent workflows using Spring beans as functions
  • Implementing structured data extraction from unstructured user inputs

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • Set up LangChain4j in my Spring Boot project
  • How to configure OpenAI models using application.properties
  • Create a Spring AI service with @AiService
  • Add a tool to my AI assistant using Spring components
  • Implement a RAG pipeline with Spring Boot and LangChain4j

Pro Tips

  • 💡Leverage Spring's `@ConfigurationProperties` to centralize and externalize all LangChain4j related configurations, including API keys and model specifics, for easier management and environment adaptation.
  • 💡Combine declarative AI services with Spring AOP for cross-cutting concerns like logging, performance monitoring, and error handling specific to AI interactions, ensuring robust observability.
  • 💡Utilize Spring's extensive testing framework to create integration tests for your AI services, mocking external LLM calls or using test doubles to ensure reliability and efficient development cycles.

What this skill does

  • Declarative interface-based AI service generation
  • Auto-configuration for multiple LLM providers via application.properties
  • Automatic dependency injection of Spring components as AI tools
  • Support for streaming responses using Project Reactor
  • Native integration with diverse vector stores for RAG workflows

When not to use it

  • Non-JVM based projects or environments without a Spring container
  • Applications where absolute minimal startup time and low memory footprint are critical
  • Simple prototyping tasks that don't require dependency injection or configuration management

Example workflow

  1. Add the core and provider-specific starters to your pom.xml or build.gradle
  2. Define API keys and model parameters in your application.properties file
  3. Create a Java interface annotated with @AiService to define agent behavior
  4. Annotate local business logic methods with @Tool for model access
  5. Inject the interface into your controller or service class to call the AI agent

Prerequisites

  • JDK 17 or higher
  • Spring Boot 3.x project
  • Active API key for chosen LLM provider

Pitfalls & limitations

  • !Hardcoding sensitive API keys in properties files instead of using environment variables
  • !Overloading the AI context with unnecessary tools, leading to token limits
  • !Failure to handle streaming exceptions when utilizing reactive patterns

FAQ

Can I use multiple AI models in one application?
Yes, you can configure multiple providers and use explicit wiring in your @AiService to select the specific model bean required for a particular task.
Does this support local LLMs like Ollama?
Yes, LangChain4j provides a dedicated starter for Ollama which can be configured alongside other providers.
How are tools registered with the AI service?
Any Spring bean method annotated with @Tool is automatically detected and registered by the AI service when it is initialized.

How it compares

Unlike manual HTTP client implementation, this starter abstracts the lifecycle of AI connections, providing type-safe interaction and automatic wiring that scales with standard Spring Boot development practices.

Source & trust

282 stars📄 MIT🕒 Updated 2026-06-15
📄 Full skill instructions — original source: giuseppe-trisciuoglio/developer-kit
# LangChain4j Spring Boot Integration

To accomplish integration of LangChain4j with Spring Boot applications, follow this comprehensive guidance covering auto-configuration, declarative AI Services, chat models, embedding stores, and production-ready patterns for building AI-powered applications.

## When to Use

To accomplish integration of LangChain4j with Spring Boot when:
- Integrating LangChain4j into existing Spring Boot applications
- Building AI-powered microservices with Spring Boot
- Setting up auto-configuration for AI models and services
- Creating declarative AI Services with Spring dependency injection
- Configuring multiple AI providers (OpenAI, Azure, Ollama, etc.)
- Implementing RAG systems with Spring Boot
- Setting up observability and monitoring for AI components
- Building production-ready AI applications with Spring Boot

## Overview

LangChain4j Spring Boot integration provides declarative AI Services through Spring Boot starters, enabling automatic configuration of AI components based on properties. The integration combines the power of Spring dependency injection with LangChain4j's AI capabilities, allowing developers to create AI-powered applications using interface-based definitions with annotations.

## Core Concepts

To accomplish basic setup of LangChain4j with Spring Boot:

**Add Dependencies:**
<!-- Core LangChain4j -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>1.8.0</version> // Use latest version
</dependency>

<!-- OpenAI Integration -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>1.8.0</version>
</dependency>


**Configure Properties:**
# application.properties
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
langchain4j.open-ai.chat-model.temperature=0.7


**Create Declarative AI Service:**
@AiService
interface CustomerSupportAssistant {
@SystemMessage("You are a helpful customer support agent for TechCorp.")
String handleInquiry(String customerMessage);
}


## Configuration

To accomplish Spring Boot configuration for LangChain4j:

**Property-Based Configuration:** Configure AI models through application properties for different providers.

**Manual Bean Configuration:** For advanced configurations, define beans manually using @Configuration.

**Multiple Providers:** Support for multiple AI providers with explicit wiring when needed.

## Declarative AI Services

To accomplish interface-based AI service definitions:

**Basic AI Service:** Create interfaces with @AiService annotation and define methods with message templates.

**Streaming AI Service:** Implement streaming responses using Reactor or Project Reactor.

**Explicit Wiring:** Specify which model to use with @AiService(wiringMode = EXPLICIT, chatModel = "modelBeanName").

## RAG Implementation

To accomplish RAG system implementation:

**Embedding Stores:** Configure various embedding stores (PostgreSQL/pgvector, Neo4j, Pinecone, etc.).

**Document Ingestion:** Implement document processing and embedding generation.

**Content Retrieval:** Set up content retrieval mechanisms for knowledge augmentation.

## Tool Integration

To accomplish AI tool integration:

**Spring Component Tools:** Define tools as Spring components with @Tool annotations.

**Database Access Tools:** Create tools for database operations and business logic.

**Tool Registration:** Automatically register tools with AI services.

## Examples

To understand implementation patterns, refer to the comprehensive examples in [references/examples.md](references/examples.md).

## Best Practices

To accomplish production-ready AI applications:

- **Use Property-Based Configuration:** External configuration over hardcoded values
- **Implement Proper Error Handling:** Graceful degradation and meaningful error responses
- **Use Profiles for Different Environments:** Separate configurations for development, testing, and production
- **Implement Proper Logging:** Debug AI service calls and monitor performance
- **Secure API Keys:** Use environment variables and never commit to version control
- **Handle Failures:** Implement retry mechanisms and fallback strategies
- **Monitor Performance:** Add metrics and health checks for observability

## References

For detailed API references, advanced configurations, and additional patterns, refer to:

- [API Reference](references/references.md) - Complete API reference and configurations
- [Examples](references/examples.md) - Comprehensive implementation examples
- [Configuration Guide](references/configuration.md) - Deep dive into configuration options

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/langchain4j-spring-boot-integration/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-spring-boot-integration/SKILL.md
  • Cursor: ~/.cursor/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-spring-boot-integration/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/giuseppe-trisciuoglio/developer-kit/langchain4j-spring-boot-integration/SKILL.md

🚀 Install with CLI:
npx skills add giuseppe-trisciuoglio/developer-kit

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid ai tools & agents issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under AI Tools & Agents and is published by Giuseppe Trisciuoglio, maintained in giuseppe-trisciuoglio/developer-kit.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.