# 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)`
  Creates a client for Anthropic's Messages API.
  `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)`
  Creates embeddings for one or more strings.
  `static LlmClient`
  `gemini(String apiKey)`
  Creates a client for Gemini's OpenAI-compatible endpoint.
  `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)`
  Creates a client for the default local Ollama endpoint.
  `static LlmClient`
  `ollama(String baseUrl, String defaultModel)`
  Creates an Ollama client for a custom endpoint.
  `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)`
  Overrides the provider endpoint for proxies or compatible services.
  `void`
  `setHttpTimeoutMs(int httpTimeoutMs)`
  Sets the network timeout for subsequent calls.

  ### 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)`.
    Parameters:
      `apiKey` - provider API key
    Returns:
      configured client

  - ### anthropic

    public static LlmClient anthropic(String apiKey)
    Creates a client for Anthropic's Messages API.
    Parameters:
      `apiKey` - Anthropic API key
    Returns:
      configured client

  - ### gemini

    public static LlmClient gemini(String apiKey)
    Creates a client for Gemini's OpenAI-compatible endpoint.
    Parameters:
      `apiKey` - Google AI API key
    Returns:
      configured client

  - ### ollama

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

  - ### ollama

    public static LlmClient ollama(String defaultModel)
    Creates a client for the default local Ollama endpoint.
    Parameters:
      `defaultModel` - model used when requests omit one
    Returns:
      local Ollama client

  - ### ollama

    public static LlmClient ollama(String baseUrl, String defaultModel)
    Creates an Ollama client for a custom endpoint.
    Parameters:
      `baseUrl` - OpenAI-compatible endpoint root
      `defaultModel` - model used when requests omit one
    Returns:
      configured Ollama client

  - ### 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.
    Parameters:
      `baseUrl` - OpenAI-compatible endpoint root
      `apiKey` - API key, or empty for an unauthenticated service
      `defaultModel` - model used when requests omit one
    Returns:
      configured compatible client

  - ### 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.
    Parameters:
      `req` - immutable chat request
    Returns:
      asynchronous normalized provider response

  - ### 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.
    Parameters:
      `req` - immutable chat request
      `listener` - incremental callbacks delivered on the EDT
    Returns:
      asynchronous aggregated response

  - ### embed

    public abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)
    Creates embeddings for one or more strings.
    Parameters:
      `req` - immutable embedding request
    Returns:
      asynchronous normalized embedding response

  - ### getProvider

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

  - ### getBaseUrl

    public String getBaseUrl()
    Returns:
      current provider endpoint root

  - ### setBaseUrl

    public void setBaseUrl(String baseUrl)
    Overrides the provider endpoint for proxies or compatible services.
    Parameters:
      `baseUrl` - endpoint root used by subsequent calls

  - ### getHttpTimeoutMs

    public int getHttpTimeoutMs()
    Returns:
      network timeout in milliseconds

  - ### setHttpTimeoutMs

    public void setHttpTimeoutMs(int httpTimeoutMs)
    Sets the network timeout for subsequent calls.
    Parameters:
      `httpTimeoutMs` - timeout in milliseconds

  - ### 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.