OpenAI-compatible API provider implementing the Provider behaviour.
Works with OpenAI, OpenRouter, Together, Groq, local vLLM — any endpoint that speaks the OpenAI chat completions format.
Configuration
Defaults to OpenAI. Override per-call via opts or globally via app config:
# Per-call
OpenAI.send(prompt, base_url: "https://openrouter.ai/api/v1",
api_key: System.get_env("OPENROUTER_API_KEY"),
model: "anthropic/claude-sonnet-4-20250514")
# Global (application config)
config :llm_core, :openai_base_url, "https://openrouter.ai/api/v1"
config :llm_core, :openai_api_key, System.get_env("OPENROUTER_API_KEY")Auth Resolution Order
opts[:api_key](per-call)Application.get_env(:llm_core, :openai_api_key)- the credential
OPENAI_API_KEY, throughLlmCore.Credentials.lookup/1(the OS environment by default; seeLlmCore.Credentialsfor a host-supplied resolver)
URL Resolution Order
opts[:base_url](per-call)Application.get_env(:llm_core, :openai_base_url)"https://api.openai.com/v1"(default)
Summary
Functions
Checks if an OpenAI-compatible API key is configured.
Checks availability using the TOML-resolved auth config.
Returns the OpenAI capability map including streaming, structured output, tool use, vision, and supported models.
Returns :api — OpenAI is a cloud API provider.
Sends a prompt to the OpenAI-compatible chat completions endpoint.
Streams a response from the OpenAI-compatible chat completions endpoint.
Types
@type stream_event() :: String.t() | {:usage, map()} | {:error, LlmCore.LLM.Error.t()}
Functions
@spec available?() :: boolean()
Checks if an OpenAI-compatible API key is configured.
Checks availability using the TOML-resolved auth config.
When a provider alias (e.g. zai) reuses this module with a different
api_key_env, this callback checks the correct env var instead of
the hardcoded OPENAI_API_KEY default.
@spec capabilities() :: LlmCore.LLM.Provider.capabilities()
Returns the OpenAI capability map including streaming, structured output, tool use, vision, and supported models.
@spec provider_type() :: :api
Returns :api — OpenAI is a cloud API provider.
@spec send(LlmCore.LLM.Provider.prompt(), keyword()) :: {:ok, LlmCore.LLM.Response.t()} | {:error, LlmCore.LLM.Error.t()}
Sends a prompt to the OpenAI-compatible chat completions endpoint.
When opts[:tools] contains a list of LlmToolkit.Tool structs, tool
definitions are encoded into the request body. If the model responds
with finish_reason: "tool_calls", the returned Response.tool_calls
will contain decoded LlmToolkit.Tool.Call structs.
opts[:response_format] requests structured output:
{:json_schema, schema}or{:json_schema, schema, name: ..., strict: ...}is sent asresponse_format: %{type: "json_schema", json_schema: %{name, schema, strict}}(strictdefaults totrue; the name defaults to the schema'stitle, else"structured_output"):json(or:json_object) is sent asresponse_format: %{type: "json_object"}- a
{:json_schema, schema}whose schema is not a map (a module, say) cannot be sent; it is requested as:json, with a warning logged once
If the endpoint answers 400 rejecting that format (its error message or param names the
response format, JSON schema/object or structured output), the call is retried once with
json_object and the schema in a system message, and if that is rejected too, once more with
the instruction in the prompt only. The reply content is decoded (a surrounding Markdown code fence is tolerated)
into Response.structured, which is nil if it is not valid JSON;
Response.metadata.response_format_mode records the mode that answered. Calls without
:response_format are unchanged.
@spec stream(LlmCore.LLM.Provider.prompt(), keyword()) :: {:ok, Enumerable.t()} | {:error, LlmCore.LLM.Error.t()}
Streams a response from the OpenAI-compatible chat completions endpoint.