# Class LlmClient

java.lang.Object
com.codename1.ai.LlmClient

public abstract class LlmClient extends Object

Provider-agnostic chat / embeddings client. Built via one of the static factory methods:

    LlmClient gpt    = LlmClient.openai("sk-...");
    LlmClient claude = LlmClient.anthropic("sk-ant-...");
    LlmClient gemini = LlmClient.gemini("AIza...");
    LlmClient ollama = LlmClient.ollama();                   // localhost:11434
    LlmClient local  = LlmClient.localOpenAiCompatible(
                           "http://10.0.0.5:8080/v1", "", "qwen2.5-7b");

All calls return `AsyncResource` so they compose naturally with the rest of the Codename One async API. `chatStream` additionally fires per-token deltas through a `StreamingListener`; both that listener and the final `AsyncResource` complete on the EDT.

##### Simulator behaviour

When running in the JavaSE simulator with `cn1.ai.simulatorRedirect` set to `ollama` (or `auto` with Ollama detected on the loopback), the static factories transparently route through a local Ollama endpoint instead of the public provider -- so unchanged production code can be debugged offline without API charges.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `DEFAULT_ANTHROPIC_URL`
  `static final String`
  `DEFAULT_GEMINI_URL`
  `static final String`
  `DEFAULT_OLLAMA_URL`
  `static final String`
  `DEFAULT_OPENAI_URL`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `LlmClient(String baseUrl)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static LlmClient`
  `anthropic(String apiKey)`
  `abstract AsyncResource<ChatResponse>`
  `chat(ChatRequest req)`
  Non-streaming chat.
  `abstract AsyncResource<ChatResponse>`
  `chatStream(ChatRequest req, StreamingListener listener)`
  Streaming chat.
  `abstract AsyncResource<EmbeddingResponse>`
  `embed(EmbeddingRequest req)`
  `static LlmClient`
  `gemini(String apiKey)`
  `String`
  `getBaseUrl()`
  `int`
  `getHttpTimeoutMs()`
  `abstract String`
  `getProvider()`
  One of `"openai"`, `"anthropic"`, `"gemini"`, `"ollama"`, `"local"`.
  `static LlmClient`
  `localOpenAiCompatible(String baseUrl, String apiKey, String defaultModel)`
  Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy).
  `static LlmClient`
  `ollama()`
  Default Ollama install: `http://localhost:11434/v1`, model `llama3.2`.
  `static LlmClient`
  `ollama(String defaultModel)`
  `static LlmClient`
  `ollama(String baseUrl, String defaultModel)`
  `static LlmClient`
  `openai(String apiKey)`
  OpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.).
  `protected String`
  `runSafetyFilter(ChatRequest req)`
  Helper for subclasses: applies the active `SafetyFilter` (if any) before the network call.
  `void`
  `setBaseUrl(String baseUrl)`
  `void`
  `setHttpTimeoutMs(int httpTimeoutMs)`

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### DEFAULT_OPENAI_URL

    public static final String DEFAULT_OPENAI_URL
    See Also:
      - Constant Field Values

  - ### DEFAULT_ANTHROPIC_URL

    public static final String DEFAULT_ANTHROPIC_URL
    See Also:
      - Constant Field Values

  - ### DEFAULT_GEMINI_URL

    public static final String DEFAULT_GEMINI_URL
    See Also:
      - Constant Field Values

  - ### DEFAULT_OLLAMA_URL

    public static final String DEFAULT_OLLAMA_URL
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### LlmClient

    protected LlmClient(String baseUrl)

- ## Method Details

  - ### openai

    public static LlmClient openai(String apiKey)
    OpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.). Uses the public endpoint by default; override with `setBaseUrl(String)`.

  - ### anthropic

    public static LlmClient anthropic(String apiKey)

  - ### gemini

    public static LlmClient gemini(String apiKey)

  - ### ollama

    public static LlmClient ollama()
    Default Ollama install: `http://localhost:11434/v1`, model `llama3.2`.

  - ### ollama

    public static LlmClient ollama(String defaultModel)

  - ### ollama

    public static LlmClient ollama(String baseUrl, String defaultModel)

  - ### localOpenAiCompatible

    public static LlmClient localOpenAiCompatible(String baseUrl, String apiKey, String defaultModel)
    Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy). `apiKey` may be empty for local services that don't authenticate.

  - ### chat

    public abstract AsyncResource<ChatResponse> chat(ChatRequest req)
    Non-streaming chat. Equivalent to `chatStream` with a no-op listener but optimized -- the provider skips the SSE response and returns a single JSON object.

  - ### chatStream

    public abstract AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener)
    Streaming chat. `listener` fires for every content delta / tool-call fragment on the EDT. The returned `AsyncResource` completes with the aggregated final response once the stream ends; cancel it to close the underlying socket.

  - ### embed

    public abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)

  - ### getProvider

    public abstract String getProvider()
    One of `"openai"`, `"anthropic"`, `"gemini"`, `"ollama"`, `"local"`. Used by `ChatView` and tests to vary behaviour by provider.

  - ### getBaseUrl

    public String getBaseUrl()

  - ### setBaseUrl

    public void setBaseUrl(String baseUrl)

  - ### getHttpTimeoutMs

    public int getHttpTimeoutMs()

  - ### setHttpTimeoutMs

    public void setHttpTimeoutMs(int httpTimeoutMs)

  - ### runSafetyFilter

    protected String runSafetyFilter(ChatRequest req)
    Helper for subclasses: applies the active `SafetyFilter` (if any) before the network call. Returns the rejection reason on failure, `null` to proceed.