{"slug": "langchain4j-cdi-best-practices", "title": "LangChain4J-CDI best practices", "summary": "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.", "body_md": "LangChain4J-CDI is the pivotal component that enables developers to integrate their AI services into their enterprise Java applications with ease, thanks to CDI.\n\nWhether 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.\n\nWhether you're creating an AI Service or an AI agent, you would feel compelled to apply the CDI scopes in this manner:\n\n```\n@RegisterAIService\n@ApplicationScoped //<-- Do not do this. It doesn't work, and there's no side effect.\npublic interface ChatAiService {\n\n    String chat(String message);\n};\n```\n\nThis 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.\n\nInstead, each of the LangChain4J-CDI stereotype annotations contains a `scope()`\n\nparameter where you can assign your CDI scope.\n\nExample:\n\n```\n@RegisterAIService(scope = ApplicationScoped.class) //Better approach, it works.\npublic interface ChatAiService {\n\n    String chat(String message);\n};\n```\n\nThis applies to all of these annotations:\n\n```\n//For AI Service\n@RegisterAIService,\n\n//For AI Agent\n@RegisterSimpleAgent,\n@RegisterSequenceAgent,\n@RegisterLoopAgent,\n@RegisterParallelAgent,\n@RegisterParallelMapperAgent,\n@RegisterConditionalAgent,\n@RegisterSupervisorAgent,\n@RegisterPlannerAgent,\n@RegisterA2AAgent,\n@RegisterMcpClientAgent,\n@RegisterHumanInTheLoopAgent\n```\n\n## 2. Using MicroProfile Fault Tolerance Retry\n\nIf you want to apply a retry policy to your AI service or AI agent using MicroProfile `@Retry`\n\nannotation, you first have to disable LangChain4J's internal retry policy (which is set to 3 retries by default).\n\nThere are various ways to do this. Using MicroProfile config, set the `maxRetries = 0`\n\n.\n\nFor example:\n\n```\ndev.langchain4j.cdi.plugin.<bean-model-name>.config.max-retries=0\n```\n\nOr, if you're using LangChain4J's provider's `ChatModelBuilder`\n\n, then you can simply apply the same value by setting `builder.maxRetries(0);`\n\n.\n\nWithout disabling LangChain4J's internal retry policy, your application server will invoke the retry with the total complexity of `O(n*m)`\n\n, where `n`\n\nis the max retries by MicroProfile and `m`\n\nis the max retries by LangChain4J.\n\nPlease note that not all LangChain4J AI providers provide a `maxRetries`\n\non their `ChatModel`\n\n. If you're declaratively configuring `maxRetries`\n\nusing MicroProfile Config on a bean without such property, the config property will be ignored.\n\nThese are the few caveats that we identified and thought were worth sharing so that you don't have to suffer in your AI discovery.\n\nHappy coding! ☕👩🏾💻", "url": "https://wpnews.pro/news/langchain4j-cdi-best-practices", "canonical_source": "https://dev.to/theelitegentleman/langchain4j-cdi-best-practices-40op", "published_at": "2026-06-27 01:51:46+00:00", "updated_at": "2026-06-27 02:34:00.339812+00:00", "lang": "en", "topics": ["developer-tools", "artificial-intelligence", "large-language-models", "ai-agents"], "entities": ["LangChain4J-CDI", "CDI", "MicroProfile", "LangChain4J"], "alternates": {"html": "https://wpnews.pro/news/langchain4j-cdi-best-practices", "markdown": "https://wpnews.pro/news/langchain4j-cdi-best-practices.md", "text": "https://wpnews.pro/news/langchain4j-cdi-best-practices.txt", "jsonld": "https://wpnews.pro/news/langchain4j-cdi-best-practices.jsonld"}}