Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Provides patterns to build declarative AI Services with LangChain4j for LLM integration, chatbot development, AI agent implementation, and conversational AI in Java. Generates type-safe AI services using interface-based patterns, annotations, memory management, and tools integration. Use when creating AI-powered Java applications with minimal boilerplate, implementing conversational AI with memory, or building AI agents with function calling.
.claude/skills/giuseppe-trisciuoglio-langchain4j-ai-services-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-05 | ✗→✓ | ▲ Improved | -9% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 23% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 28% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 90% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 44% | 0% |
This skill provides guidance for building declarative AI Services with LangChain4j using interface-based patterns, annotations for system and user messages, memory management, tools integration, and advanced AI application patterns that abstract away low-level LLM interactions.
LangChain4j AI Services define AI functionality using Java interfaces with annotations, providing type-safe, declarative AI with minimal boilerplate.
Use this skill when:
Follow these steps to create declarative AI Services with LangChain4j:
Create a Java interface with method signatures for AI interactions:
javainterface Assistant { String chat(String userMessage); }
Use @SystemMessage and @UserMessage annotations to define prompts:
javainterface CustomerSupportBot { @SystemMessage("You are a helpful customer support agent for TechCorp") String handleInquiry(String customerMessage); @UserMessage("Analyze sentiment: {{it}}") Sentiment analyzeSentiment(String feedback); }
Use AiServices builder or create to instantiate the service:
java// Simple creation Assistant assistant = AiServices.create(Assistant.class, chatModel); // Or with builder for advanced configuration Assistant assistant = AiServices.builder(Assistant.class) .chatModel(chatModel) .build();
Add memory management using @MemoryId for multi-user scenarios:
javainterface MultiUserAssistant { String chat(@MemoryId String userId, String userMessage); } Assistant assistant = AiServices.builder(MultiUserAssistant.class) .chatModel(model) .chatMemoryProvider(userId -> MessageWindowChatMemory.withMaxMessages(10)) .build();
Register tools using @Tool annotation to enable AI function execution:
javaclass Calculator { @Tool("Add two numbers") double add(double a, double b) { return a + b; } } interface MathGenius { String ask(String question); } MathGenius mathGenius = AiServices.builder(MathGenius.class) .chatModel(model) .tools(new Calculator()) .build();
Test AI services with concrete validation patterns:
java// 1. Test with sample inputs String response = assistant.chat("Hello, how are you?"); assert response != null && !response.isEmpty(); // 2. Validate structured outputs with assertions Sentiment result = bot.analyzeSentiment("Great product!"); assert result == Sentiment.POSITIVE; // 3. Log tool calls with side effects for audit MathGenius math = AiServices.builder(MathGenius.class) .chatModel(model) .tools(new Calculator()) .build(); // 4. Test memory isolation between users String userA = assistant.chat("User A message", "session-a"); String userB = assistant.chat("User B message", "session-b"); assert !userA.equals(userB); // Verify memory isolation
See examples.md for comprehensive practical examples including:
Complete API documentation, annotations, interfaces, and configuration patterns are available in references.md.
xml<!-- Maven --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai</artifactId> <version>1.8.0</version> </dependency>
gradle// Gradle implementation 'dev.langchain4j:langchain4j:1.8.0' implementation 'dev.langchain4j:langchain4j-open-ai:1.8.0'
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 10,074 | 4,770 | -53% | 1 | 1 | 0% | 1,867 | 2,295 | +23% | 0 | 0 | — |
case-02 | pass→pass | 9,289 | 3,949 | -57% | 1 | 1 | 0% | 1,604 | 2,060 | +28% | 0 | 0 | — |
case-03 | pass→pass | 8,981 | 12,615 | +40% | 1 | 1 | 0% | 1,608 | 3,049 | +90% | 0 | 0 | — |
case-04 | pass→pass | 8,728 | 5,003 | -43% | 1 | 1 | 0% | 1,569 | 2,252 | +44% | 0 | 0 | — |
case-05 | fail→pass | 14,744 | 6,861 | -53% | 1 | 1 | 0% | 2,798 | 2,544 | -9% | 0 | 0 | — |
case-06 | pass→pass | 10,307 | 7,421 | -28% | 1 | 1 | 0% | 1,935 | 2,596 | +34% | 0 | 0 | — |
case-07 | pass→pass | 8,080 | 3,973 | -51% | 1 | 1 | 0% | 1,473 | 2,048 | +39% | 0 | 0 | — |
case-08 | pass→pass | 7,885 | 5,303 | -33% | 1 | 1 | 0% | 1,396 | 2,312 | +66% | 0 | 0 | — |
case-09 | pass→pass | 11,088 | 8,903 | -20% | 1 | 1 | 0% | 2,109 | 3,040 | +44% | 0 | 0 | — |
case-10 | fail→fail | 14,789 | 9,583 | -35% | 1 | 1 | 0% | 2,693 | 3,244 | +20% | 0 | 0 | — |
case-11 | pass→pass | 5,744 | 1,674 | -71% | 1 | 1 | 0% | 1,037 | 1,655 | +60% | 0 | 0 | — |
case-12 | pass→pass | 5,937 | 2,805 | -53% | 1 | 1 | 0% | 1,061 | 1,784 | +68% | 0 | 0 | — |
case-13 | pass→pass | 15,025 | 10,038 | -33% | 1 | 1 | 0% | 2,350 | 3,069 | +31% | 0 | 0 | — |
case-14 | pass→pass | 13,911 | 6,140 | -56% | 1 | 1 | 0% | 1,279 | 2,447 | +91% | 0 | 0 | — |
case-15 | pass→pass | 14,354 | 12,054 | -16% | 1 | 1 | 0% | 2,319 | 3,433 | +48% | 0 | 0 | — |
case-16 | pass→pass | 15,706 | 5,567 | -65% | 1 | 1 | 0% | 1,456 | 2,368 | +63% | 0 | 0 | — |
case-17 | pass→pass | 12,398 | 7,569 | -39% | 1 | 1 | 0% | 2,085 | 2,561 | +23% | 0 | 0 | — |
case-18 | pass→pass | 15,892 | 10,811 | -32% | 1 | 1 | 0% | 2,585 | 3,148 | +22% | 0 | 0 | — |
case-19 | pass→pass | 6,163 | 5,836 | -5% | 1 | 1 | 0% | 963 | 2,443 | +154% | 0 | 0 | — |
case-20 | pass→pass | 10,908 | 10,029 | -8% | 1 | 1 | 0% | 2,096 | 3,283 | +57% | 0 | 0 | — |
case-21 | pass→pass | 6,203 | 6,978 | +12% | 1 | 1 | 0% | 1,127 | 2,608 | +131% | 0 | 0 | — |
case-22 | pass→pass | 5,018 | 5,929 | +18% | 1 | 1 | 0% | 873 | 2,510 | +188% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.