Menu

Your First LangChain4j Chat: Connecting Java to Anthropic Claude
📰

Your First LangChain4j Chat: Connecting Java to Anthropic Claude

DEV Community: ai·Said Olano·9 days ago
#OWqGcPJz
#devtoai#devto#dev#model#langchain4j
Long article (2717 words) — no AI summary yet.
Reading 0:00
15s threshold

Calling a large language model from Java used to mean writing HTTP clients, parsing JSON by hand and wiring retries yourself. LangChain4j removes most of that plumbing. In this article we build the smallest useful program: a Java 21 application that creates a Claude chat model with LangChain4j and sends a single prompt. The code is small on purpose, so every line has a clear job, and the structure is the same one you will reuse for tools, memory, retrieval and agents later.

The complete source is in github.com/josesaid/01-chatmodel-anthropic , and a project page with the diagrams is available at josesaid.github.io/01-chatmodel-anthropic .

What you will build

The application does four things:

Reads the Anthropic API key from an environment variable.

Builds an AnthropicChatModel with the builder pattern.

Sends one question with chat(String) .

Prints the question and the answer.

The flow looks like this:

Your Java app -> AnthropicChatModel (LangChain4j) -> Claude API chat(prompt) HTTPS request answer…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.