Monday, 22 December 2025

Getting Started with Agents in LangChain4j

When working with Large Language Models (LLMs), you often want them to act like “specialists” that can perform a well-defined task, whether it’s writing a poem, summarizing a document, or answering a customer’s question.

In LangChain4j, these specialists are called agents. An agent is simply an interface that defines a task and lets the LLM perform it. The magic comes from how simple it is to define and connect an agent to your system.

 

What is an Agent?

An agent in LangChain4j is a component that:

 

·      Performs a specific task using an LLM.

·      Is defined as a Java interface.

·      Uses special annotations like @Agent and @UserMessage to describe its purpose and instructions.

 

Think of an agent as a helper bot with a clear job description.

 

Example: A Poet Agent

Here’s a simple agent that writes poems:

public interface Poet {

    @UserMessage("""
            You are a poet.
            Write a short poem of no more than
            4 lines about the given theme.
            Return only the poem and nothing else.
            The theme is {{theme}}.
            """)
    @Agent("Generates a short poem based on the given theme")
    String generatePoem(@V("theme") String theme);
}

 

Here:

·      @UserMessage: Defines what instructions the LLM should follow.

·      @Agent: Declares this as an agent and gives it a description.

·      Method Parameter @V("theme"): Passes the theme (like "friendship" or "nature") into the message template.

 

So, when you call generatePoem("friendship"), the agent will ask the LLM to produce a short poem about friendship.

 

Why Provide a Description?

The description tells other agents or systems what this agent does. This is especially useful in multi-agent systems, where several agents collaborate. You can even supply the description about the agent while constructing an Agent object using AgenticServices.

Poet poet = AgenticServices.agentBuilder(Poet.class)
        .description("Generates a short poem based on the given theme")
        .chatModel(chatModel)
        .build();

Naming Your Agent

Every agent should have a unique name in the system. This can be set in two ways:

 

·      In the @Agent annotation.

·      Programmatically, using .name("Poet") when building the agent.

Poet poet = AgenticServices.agentBuilder(Poet.class)
        .description("Generates a short poem based on the given theme")
        .name("Poet")
        .chatModel(chatModel)
        .build();

If you don’t set a name, LangChain4j uses the method name (generatePoem) as the agent’s name.

 

Find the below working Application.

 

Poet.java

 

package com.sample.app.agents;

import dev.langchain4j.agentic.Agent;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;

public interface Poet {

  @UserMessage(
      """
            You are a poet.
            Write a short poem of no more than
            4 lines about the given theme.
            Return only the poem and nothing else.
            The theme is {{theme}}.
            """)
  @Agent(description = "Generates a short poem based on the given theme", name = "Poet")
  String generatePoem(@V("theme") String theme);
}

AgentHelloWorld.java

package com.sample.app;

import com.sample.app.agents.Poet;
import dev.langchain4j.agentic.AgenticServices;
import dev.langchain4j.model.ollama.OllamaChatModel;

public class AgentHelloWorld {

  public static void main(String[] args) {

    // Initialize LLM model
    OllamaChatModel chatModel =
        OllamaChatModel.builder().baseUrl("http://localhost:11434").modelName("llama3.2").build();

    Poet poet = AgenticServices.agentBuilder(Poet.class).chatModel(chatModel).build();

    String poem = poet.generatePoem("nature");

    System.out.println(poem);
  }
}

Output

Golden sunsets fade to blue,
Nature's canvas, painted anew.
Trees stand tall, their leaves rustle free,
A symphony for you and me.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment