Prev Next

AI / LangChain4j interview questions

1. What is LangChain4j and what problem does it solve for Java developers? 2. What are the core modules of LangChain4j? 3. What is the AI Services feature in LangChain4j and how do you define one? 4. How does ChatMemory work in LangChain4j and what types are available? 5. What is Retrieval-Augmented Generation (RAG) in LangChain4j and how do you build a pipeline? 6. What are Tools in LangChain4j and how does tool calling work? 7. How do you integrate LangChain4j with Spring Boot? 8. What is the EmbeddingModel in LangChain4j and which providers are supported? 9. What EmbeddingStores does LangChain4j support and how do you choose one? 10. What is document splitting in LangChain4j and why is it necessary? 11. What is the @SystemMessage and @UserMessage annotation in LangChain4j AI Services? 12. How does streaming work in LangChain4j and when should you use it? 13. What is the ContentRetriever and RetrievalAugmentor in LangChain4j advanced RAG? 14. How does LangChain4j handle structured output from LLMs? 15. What is the PromptTemplate in LangChain4j and how does it differ from @UserMessage? 16. What LLM providers does LangChain4j support and how do you switch between them? 17. What is an Agent in LangChain4j and how does it differ from a simple AI Services call? 18. How do you implement multi-turn conversation with memory per user in a Spring REST API using LangChain4j? 19. What is the ImageModel in LangChain4j and which providers support image generation? 20. How do you handle errors and retries in LangChain4j? 21. How do you test LangChain4j AI Services without making real LLM API calls? 22. What is the DocumentLoader API in LangChain4j and what sources does it support? 23. What is the @Moderate annotation in LangChain4j and how does content moderation work? 24. How does LangChain4j support vision (multi-modal) LLMs that accept images as input? 25. What is the difference between synchronous and asynchronous execution in LangChain4j? 26. What is LangChain4j's support for Quarkus and how does it differ from Spring Boot integration? 27. How does LangChain4j implement the ReAct agent pattern and what are its limitations? 28. What is the ModerationModel interface in LangChain4j and how can you implement a custom one? 29. What is the Tokenizer interface in LangChain4j and why does it matter for memory management? 30. How do you persist ChatMemory across application restarts in LangChain4j? 31. What are the best practices for prompt engineering within LangChain4j AI Services? 32. How does LangChain4j integrate with observability tools like OpenTelemetry? 33. What is the InMemoryEmbeddingStore and when should you migrate to a real vector database? 34. What are common LangChain4j anti-patterns to avoid in production applications? 35. How does LangChain4j support multi-modal input processing for audio or documents beyond text and images? 36. How do you implement a custom Tool with complex parameter types in LangChain4j? 37. What is the HypotheticalDocumentEmbedder (HyDE) technique and how does LangChain4j support it? 38. How do you handle LLM output parsing failures gracefully in LangChain4j? 39. What is LangChain4j's support for graph-based RAG or knowledge graph integration? 40. What is the LangChain4j EvaluationResult API and how do you measure RAG pipeline quality?

1. What is LangChain4j and what problem does it solve for Java developers?

LangChain4j is a Java library that brings the capabilities of large language models (LLMs) into the Java ecosystem in a structured, type-safe, and production-friendly way. Before LangChain4j, Java developers who wanted to integrate GPT, Gemini, Mistral, or any other LLM into their applications ha...

Read full answer

2. What are the core modules of LangChain4j?

LangChain4j is organized into several Maven modules so you only pull in what you actually need. The main ones you will encounter in real projects are: LangChain4j Core Modules Module Artifact ID Purpose Core langchain4j-core Interfaces and abstractions (ChatLanguageModel, EmbeddingModel, ChatMemo...

Read full answer

3. What is the AI Services feature in LangChain4j and how do you define one?

AI Services is the flagship abstraction in LangChain4j. The idea is simple but powerful: you write a plain Java interface, annotate its methods with LangChain4j annotations that describe what each method should do with the LLM, and the library generates a working implementation at runtime using J...

Read full answer

4. How does ChatMemory work in LangChain4j and what types are available?

ChatMemory in LangChain4j is the component responsible for maintaining conversation history across multiple exchanges with an LLM. Without it, every call to the model is stateless — the model has no knowledge of what was said in previous turns. ChatMemory solves this by accumulating the message h...

Read full answer

5. What is Retrieval-Augmented Generation (RAG) in LangChain4j and how do you build a pipeline?

RAG (Retrieval-Augmented Generation) is the technique of enriching an LLM prompt with relevant external content retrieved from a knowledge base before asking the model to generate a response. It solves the core limitation of LLMs — their knowledge is frozen at training time — by dynamically injec...

Read full answer

6. What are Tools in LangChain4j and how does tool calling work?

Tools (also called function calling) give LLMs the ability to invoke real Java methods during a conversation. Instead of answering entirely from its training knowledge, the model can recognize when a specific capability is needed — fetching live data, running calculations, calling APIs — and requ...

Read full answer

7. How do you integrate LangChain4j with Spring Boot?

LangChain4j provides a dedicated Spring Boot starter ( langchain4j-spring-boot-starter ) that wires everything up through standard Spring Boot auto-configuration. You add the starter plus the provider-specific starter for your chosen LLM, drop configuration into application.properties , and Sprin...

Read full answer

8. What is the EmbeddingModel in LangChain4j and which providers are supported?

An EmbeddingModel in LangChain4j converts text into dense numerical vectors (embeddings) that capture semantic meaning. Texts with similar meanings produce vectors that are geometrically close, enabling similarity search. EmbeddingModels are used during RAG ingestion (to vectorize document chunks...

Read full answer

9. What EmbeddingStores does LangChain4j support and how do you choose one?

An EmbeddingStore is the vector database layer in LangChain4j's RAG pipeline — it stores embedding vectors alongside their source text and metadata, and supports approximate nearest-neighbor (ANN) similarity search. LangChain4j implements a unified EmbeddingStore interface across all...

Read full answer

10. What is document splitting in LangChain4j and why is it necessary?

Document splitting (also called chunking) is the process of dividing a large document into smaller, overlapping segments before embedding and storing them in the vector database. It is a necessary step in RAG pipelines because LLMs have a fixed context window (e.g., 8K, 32K, or 128K tokens). You ...

Read full answer

11. What is the @SystemMessage and @UserMessage annotation in LangChain4j AI Services?

@SystemMessage and @UserMessage are the two prompt-definition annotations at the core of LangChain4j's AI Services pattern. Together they define what gets sent to the LLM for each method invocation, replacing all manual prompt string assembly. @SystemMessage defines the system prompt — the person...

Read full answer

12. How does streaming work in LangChain4j and when should you use it?

Streaming in LangChain4j allows the LLM's response to be delivered token-by-token as it is generated, rather than waiting for the entire response to be produced before returning anything to the caller. For user-facing chat interfaces, this dramatically improves perceived responsiveness — the user...

Read full answer

13. What is the ContentRetriever and RetrievalAugmentor in LangChain4j advanced RAG?

LangChain4j's advanced RAG API introduces a cleaner abstraction hierarchy above the basic EmbeddingStoreContentRetriever . The two key interfaces are ContentRetriever and RetrievalAugmentor . ContentRetriever is the interface responsible for fetching relevant content given a query. Multiple imple...

Read full answer

14. How does LangChain4j handle structured output from LLMs?

Structured output means getting the LLM to return data that maps directly to a Java object — a POJO, record, enum, or collection — rather than free-form text that you parse yourself. LangChain4j makes this transparent: declare the return type of your AI Services method as the desired Java type, a...

Read full answer

15. What is the PromptTemplate in LangChain4j and how does it differ from @UserMessage?

PromptTemplate is the lower-level prompt construction API in LangChain4j, used when you are working directly with ChatLanguageModel or building custom chains without the AI Services abstraction. It lets you define a reusable template string with {{variable}} placeholders and fill them in programm...

Read full answer

16. What LLM providers does LangChain4j support and how do you switch between them?

LangChain4j supports a wide range of LLM providers, both cloud-based and local, through its modular dependency design. Each provider is a separate Maven module that implements the core ChatLanguageModel and optionally EmbeddingModel , StreamingChatLanguageModel , and ImageModel interfaces. LangCh...

Read full answer

17. What is an Agent in LangChain4j and how does it differ from a simple AI Services call?

In LangChain4j, an Agent is an AI Services instance that has been equipped with a set of Tools and operates in an autonomous reasoning loop. Instead of a single-shot prompt-and-respond interaction, an agent decides at each step whether to answer directly from its knowledge or to invoke one of the...

Read full answer

18. How do you implement multi-turn conversation with memory per user in a Spring REST API using LangChain4j?

Implementing per-user conversational memory in a Spring REST API requires three things: an AI Services interface with a memory-id parameter, a ChatMemoryProvider that returns isolated memory per ID, and a backing store to persist conversations across requests (or restarts). // 1. AI Services inte...

Read full answer

19. What is the ImageModel in LangChain4j and which providers support image generation?

ImageModel is the LangChain4j interface for text-to-image generation — sending a text prompt and receiving a generated image in return. It follows the same provider-abstraction pattern as ChatLanguageModel: your code works against the interface, and the actual generation is delegated to whichever...

Read full answer

20. How do you handle errors and retries in LangChain4j?

LangChain4j itself does not provide a built-in retry framework — it intentionally delegates retry logic to the infrastructure layer. However, there are several natural integration points for error handling depending on your deployment context. Rate limit handling (HTTP 429) — Most provider implem...

Read full answer

21. How do you test LangChain4j AI Services without making real LLM API calls?

Testing AI Services without hitting real LLM endpoints is essential for fast, cost-free, deterministic unit tests. LangChain4j supports this through mock model implementations and the AiServices builder accepting any ChatLanguageModel — including test doubles you create yourself. The most direct ...

Read full answer

22. What is the DocumentLoader API in LangChain4j and what sources does it support?

Document loaders are the entry point of any RAG ingestion pipeline — they read raw content from a source and return it as a list of Document objects, each containing the text content and source metadata. LangChain4j's loaders all implement the DocumentLoader interface and populate the Document.me...

Read full answer

23. What is the @Moderate annotation in LangChain4j and how does content moderation work?

The @Moderate annotation integrates content moderation directly into the AI Services pipeline. When placed on an AI Services method, LangChain4j automatically runs the user message through OpenAI's Moderation API before passing it to the language model. If the content is flagged as violating cont...

Read full answer

24. How does LangChain4j support vision (multi-modal) LLMs that accept images as input?

Multi-modal LLMs like GPT-4o, Claude 3, and Gemini can process images alongside text. In LangChain4j, image input is handled through the UserMessage content builder, which accepts a list of Content objects — combining TextContent and ImageContent in a single user turn. // Pass an image URL UserMe...

Read full answer

25. What is the difference between synchronous and asynchronous execution in LangChain4j?

LangChain4j supports both synchronous and asynchronous execution models for LLM calls. The choice affects how your application thread behaves while waiting for the (potentially slow) LLM response. Synchronous — The calling thread blocks until the complete response is received. This is the default...

Read full answer

26. What is LangChain4j's support for Quarkus and how does it differ from Spring Boot integration?

LangChain4j has a dedicated Quarkus extension ( quarkus-langchain4j ) maintained under the Quarkiverse umbrella. It provides CDI-based injection, Quarkus-native configuration, and — critically — native compilation support through GraalVM, enabling LangChain4j applications to be compiled to native...

Read full answer

27. How does LangChain4j implement the ReAct agent pattern and what are its limitations?

The ReAct (Reasoning + Acting) pattern in LangChain4j is implemented automatically by the AI Services framework whenever you register tools with a chat language model. There is no explicit ReAct class to instantiate — the pattern emerges from the interaction between the tool-equipped LLM and Lang...

Read full answer

28. What is the ModerationModel interface in LangChain4j and how can you implement a custom one?

The ModerationModel interface in LangChain4j defines the contract for content moderation checks. It takes a String input and returns a Response — where Moderation contains a boolean flagged() result and optionally category-level scores. LangChain4j's @Moderate AI Services annotation u...

Read full answer

29. What is the Tokenizer interface in LangChain4j and why does it matter for memory management?

The Tokenizer interface in LangChain4j counts the number of tokens in a given string or list of messages using the specific tokenization algorithm of a target model. This is necessary because LLMs do not process raw characters or words — they operate on tokens, which are sub-word units that vary ...

Read full answer

30. How do you persist ChatMemory across application restarts in LangChain4j?

LangChain4j's built-in MessageWindowChatMemory and TokenWindowChatMemory use in-memory storage — conversations vanish when the application restarts or when a new pod starts in a Kubernetes cluster. For production persistence you need a persistent ChatMemoryStore implementation. LangChain4j define...

Read full answer

31. What are the best practices for prompt engineering within LangChain4j AI Services?

Prompt engineering in LangChain4j is about designing the @SystemMessage and @UserMessage content so the LLM reliably produces what you need. Several practices have proven effective in production LangChain4j applications: 1. Keep system messages focused and specific. A system message that tries to...

Read full answer

32. How does LangChain4j integrate with observability tools like OpenTelemetry?

LangChain4j 0.31+ introduced native OpenTelemetry instrumentation for tracing LLM calls. When the langchain4j-open-telemetry module is on the classpath alongside an OTel SDK, LangChain4j automatically creates spans for each LLM call, embedding attributes from the OpenTelemetry Semantic Convention...

Read full answer

33. What is the InMemoryEmbeddingStore and when should you migrate to a real vector database?

InMemoryEmbeddingStore is LangChain4j's simplest EmbeddingStore implementation: it holds all embeddings in a Java List in heap memory, performs linear scan (brute-force cosine similarity) for similarity search, and has zero external dependencies. It ships in the core module with no additional Mav...

Read full answer

34. What are common LangChain4j anti-patterns to avoid in production applications?

As LangChain4j adoption has grown, several recurring mistakes in production deployments have emerged. Knowing these saves debugging time and prevents costly incidents. 1. Creating ChatLanguageModel or AI Services as request-scoped beans. These are expensive to initialize (TCP connections, key val...

Read full answer

35. How does LangChain4j support multi-modal input processing for audio or documents beyond text and images?

Beyond text and image inputs, some LLM providers support audio transcription and document (PDF) understanding as native model inputs. LangChain4j exposes these through additional Content types in the UserMessage builder, following the same pattern as ImageContent . Audio input — For providers tha...

Read full answer

36. How do you implement a custom Tool with complex parameter types in LangChain4j?

LangChain4j tools support complex parameter types beyond simple strings and primitives. When a tool method accepts a custom POJO, enum, or collection, LangChain4j automatically generates a JSON schema from the parameter type and includes it in the tool specification sent to the LLM. The model use...

Read full answer

37. What is the HypotheticalDocumentEmbedder (HyDE) technique and how does LangChain4j support it?

HyDE (Hypothetical Document Embedder) is a query enhancement technique for RAG that improves retrieval quality by addressing a fundamental mismatch: the user's question is short and query-like, while the stored documents are long and answer-like. Embedding a question and a document paragraph in t...

Read full answer

38. How do you handle LLM output parsing failures gracefully in LangChain4j?

When LangChain4j requests structured output (returning a POJO from an AI Services method), the LLM occasionally produces malformed JSON despite format instructions — especially with smaller models or complex schemas. Without explicit error handling, this surfaces as a OutputParsingException or Js...

Read full answer

39. What is LangChain4j's support for graph-based RAG or knowledge graph integration?

Standard vector similarity RAG retrieves semantically similar text chunks, but it struggles with multi-hop reasoning — questions like "What are all the direct reports of the manager of the product that had the most returns in Q3?" require traversing multiple relationships, not just finding simila...

Read full answer

40. What is the LangChain4j EvaluationResult API and how do you measure RAG pipeline quality?

RAG pipeline quality is notoriously hard to measure because "good retrieval" and "good answers" are context-dependent and partially subjective. LangChain4j does not provide a built-in RAG evaluation framework, but the ecosystem approach involves using LLMs themselves as evaluators (LLM-as-judge) ...

Read full answer

«
»

Comments & Discussions