cd /news/developer-tools/langchain4j-cdi-best-practices · home topics developer-tools article
[ARTICLE · art-41517] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

LangChain4J-CDI best practices

LangChain4J-CDI enables integration of AI services into enterprise Java applications using CDI. Best practices include avoiding direct CDI scope annotations on AI service interfaces and instead using the scope parameter in stereotype annotations like @RegisterAIService. For MicroProfile Fault Tolerance Retry, developers must disable LangChain4J's internal retry policy to prevent exponential retry complexity.

read2 min views1 publishedJun 27, 2026

LangChain4J-CDI is the pivotal component that enables developers to integrate their AI services into their enterprise Java applications with ease, thanks to CDI.

Whether you're a seasoned developer or getting started with integrating AI in your existing Java application, here are the best practices that will ensure you get building your AI solutions with ease.

Whether you're creating an AI Service or an AI agent, you would feel compelled to apply the CDI scopes in this manner:

@RegisterAIService
@ApplicationScoped //<-- Do not do this. It doesn't work, and there's no side effect.
public interface ChatAiService {

    String chat(String message);
};

This won't work as the interface is not CDI managed. For those who want to be too technical, the CD scopes cannot propagate down to its proxy.

Instead, each of the LangChain4J-CDI stereotype annotations contains a scope()

parameter where you can assign your CDI scope.

Example:

@RegisterAIService(scope = ApplicationScoped.class) //Better approach, it works.
public interface ChatAiService {

    String chat(String message);
};

This applies to all of these annotations:

//For AI Service
@RegisterAIService,

//For AI Agent
@RegisterSimpleAgent,
@RegisterSequenceAgent,
@RegisterLoopAgent,
@RegisterParallelAgent,
@RegisterParallelMapperAgent,
@RegisterConditionalAgent,
@RegisterSupervisorAgent,
@RegisterPlannerAgent,
@RegisterA2AAgent,
@RegisterMcpClientAgent,
@RegisterHumanInTheLoopAgent

2. Using MicroProfile Fault Tolerance Retry #

If you want to apply a retry policy to your AI service or AI agent using MicroProfile @Retry

annotation, you first have to disable LangChain4J's internal retry policy (which is set to 3 retries by default).

There are various ways to do this. Using MicroProfile config, set the maxRetries = 0

.

For example:

dev.langchain4j.cdi.plugin.<bean-model-name>.config.max-retries=0

Or, if you're using LangChain4J's provider's ChatModelBuilder

, then you can simply apply the same value by setting builder.maxRetries(0);

.

Without disabling LangChain4J's internal retry policy, your application server will invoke the retry with the total complexity of O(n*m)

, where n

is the max retries by MicroProfile and m

is the max retries by LangChain4J.

Please note that not all LangChain4J AI providers provide a maxRetries

on their ChatModel

. If you're declaratively configuring maxRetries

using MicroProfile Config on a bean without such property, the config property will be ignored.

These are the few caveats that we identified and thought were worth sharing so that you don't have to suffer in your AI discovery.

Happy coding! ☕👩🏾💻

── more in #developer-tools 4 stories · sorted by recency
── more on @langchain4j-cdi 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/langchain4j-cdi-best…] indexed:0 read:2min 2026-06-27 ·