Java SDK

Use Nemo Router with the official OpenAI Java SDK

Last updated

Nemo Router works with the official openai-java SDK. Point the client at https://api.nemorouter.ai/v1 and pass your sk-nemo-... key — guardrails, caching, and rate-limits auto-apply from your org config.

Installation

Add to your pom.xml:

<dependency>
  <groupId>com.openai</groupId>
  <artifactId>openai-java</artifactId>
  <version>2.2.0</version>
</dependency>

Setup

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;

OpenAIClient client = OpenAIOkHttpClient.builder()
    .apiKey(System.getenv("NEMOROUTER_API_KEY"))
    .baseUrl("https://api.nemorouter.ai/v1")
    .build();

Chat Completion

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;

public class NemoRouterExample {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
            .apiKey(System.getenv("NEMOROUTER_API_KEY"))
            .baseUrl("https://api.nemorouter.ai/v1")
            .build();

        ChatCompletion response = client.chat().completions().create(
            ChatCompletionCreateParams.builder()
                .model("claude-sonnet-4-20250514")
                .addMessage(ChatCompletionMessageParam.ofUser(
                    ChatCompletionUserMessageParam.builder()
                        .content("Hello!")
                        .build()
                ))
                .build()
        );
        System.out.println(response.choices().get(0).message().content());
    }
}

Per-Request Overrides

The Java SDK does not yet support extra_body fields natively. To pass nemo_* fields per request (guardrails, prompt templates, cache toggle), use java.net.http.HttpClient and include them in the raw JSON body:

{
  "model": "gpt-4o",
  "messages": [{"role": "user", "content": "Summarize Q1 earnings..."}],
  "nemo_guardrail_ids": ["uuid1", "uuid2"],
  "nemo_prompt_template_id": "your-summarizer-id",
  "nemo_prompt_variables": {"language": "Spanish"},
  "nemo_cache": false
}

Error Handling

try {
    client.chat().completions().create(/* ... */);
} catch (Exception e) {
    // "guardrail_blocked" — guardrail rejected the request
    // "insufficient_credits" — top up to continue
    System.out.println("Error: " + e.getMessage());
}

Response Headers

Every successful response carries:

  • x-nemo-request-cost — exact USD spend for this call
  • x-nemo-guardrails-applied — comma-separated guardrail names that ran
  • x-nemo-prompt-version — version of the prompt template applied (if any)

Next Steps

Was this page helpful?