# `Alloy.Provider.OpenAICompat`
[🔗](https://github.com/alloy-ex/alloy/blob/v0.12.4/lib/alloy/provider/openai_compat.ex#L1)

Generic OpenAI-compatible provider.

Works with any API that implements the OpenAI chat completions format:
DeepSeek, Mistral, xAI/Grok, Ollama, OpenRouter, Together, Groq, etc.

## Config

Required:
- `:api_url` - Base URL (e.g., "https://api.deepseek.com",
  "https://api.mistral.ai", "https://api.x.ai", "http://localhost:11434")
- `:model` - Model name

Optional:
- `:api_key` - API key (omit for local providers like Ollama)
- `:max_tokens` - Max output tokens (default: 4096)
- `:system_prompt` - System prompt string
- `:chat_path` - Path to completions endpoint (default: "/v1/chat/completions")
- `:extra_headers` - Additional headers as `[{name, value}]`
- `:req_options` - Additional options passed to Req

## Examples

    # DeepSeek
    Alloy.run("Hello",
      provider: {Alloy.Provider.OpenAICompat,
        api_key: System.get_env("DEEPSEEK_API_KEY"),
        api_url: "https://api.deepseek.com",
        model: "deepseek-chat"
      }
    )

    # Ollama (no API key)
    Alloy.run("Hello",
      provider: {Alloy.Provider.OpenAICompat,
        api_url: "http://localhost:11434",
        model: "llama4"
      }
    )

    # xAI chat completions compatibility
    Alloy.run("Hello",
      provider: {Alloy.Provider.OpenAICompat,
        api_key: System.get_env("XAI_API_KEY"),
        api_url: "https://api.x.ai",
        model: "grok-code-fast-1"
      }
    )

    # Mistral (mistral-large-latest and -2512 are in the built-in
    # model catalog, so context budgeting works out of the box)
    Alloy.run("Hello",
      provider: {Alloy.Provider.OpenAICompat,
        api_key: System.get_env("MISTRAL_API_KEY"),
        api_url: "https://api.mistral.ai",
        model: "mistral-large-latest"
      }
    )

# `config`

```elixir
@type config() :: %{
  :api_url =&gt; String.t(),
  :model =&gt; String.t(),
  optional(:api_key) =&gt; String.t(),
  optional(:max_tokens) =&gt; pos_integer(),
  optional(:system_prompt) =&gt; String.t(),
  optional(:chat_path) =&gt; String.t(),
  optional(:extra_headers) =&gt; [{String.t(), String.t()}],
  optional(:req_options) =&gt; keyword()
}
```

Configuration for the OpenAI-compatible provider. `:api_key` is optional
(omit for local providers like Ollama). See the module doc for field
semantics.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
