Documentation

LangChain4j

How to use GATE/0 with LangChain4j

LangChain4j is an open-source Java library that simplifies the integration of LLMs into Java applications.

GATE/0 integrates seamlessly with LangChain4j by leveraging its support for custom API endpoints and authentication keys. This allows you to route all LLM calls through GATE/0 with minimal setup — unlocking detailed usage analytics, labeling, and cost tracking — without altering how your chains are structured.

Using GATE/0 with LangChain4j

Set up your OpenAI provider in GATE/0

  1. Retrieve your OpenAI API key from OpenAI
  2. Add a new OpenAI provider in GATE/0 here
  3. Copy your GATE/0 API key from here

Install LangChain4J

<dependency>
  <groupId>dev.langchain4j</groupId>
  <artifactId>langchain4j-open-ai</artifactId>
  <version>1.4.0</version>
</dependency>
implementation 'dev.langchain4j:langchain4j-open-ai:1.4.0'
implementation 'dev.langchain4j:langchain4j:1.4.0'

Configure the GATE/0 API key Set your GATE/0 API key as an environment variable:

export GATE0_API_KEY=your-gate0-api-key

Start using LangChain4J

import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import java.util.Map;

public class LangChain4jDemo {
    public static void main(String[] args) {

        ChatModel chatModel = OpenAiChatModel.builder()
                    .baseUrl("https://gateway.gate.io/v1")
                    .apiKey(System.getenv("GATE0_API_KEY"))
                    .modelName("openai/gpt-4o")
                    .metadata(Map.ofEntries(
                        Map.entry("gate0_label_project", "alpha"),
                        Map.entry("gate0_label_client", "java"),
                        Map.entry("gate0_label_env", "prod")))
                    .build();

        String joke = chatModel.chat("Tell me a joke about Java");

        System.out.println(joke);
    }
}

Explanation of the implementation

An instance of ChatModel is build using OpenAiChatModel.builder(), with:

  1. Base URL set to https://gateway.gate0.io/v1
  2. A model name prefixed with the provider slug (e.g., openai/gpt-4o instead of gpt-4o)
  3. Your GATE/0 API key passed via headers or environment
  4. Custom labels passed as a Map to metadata