# Class LittleGhost::Providers::OpenAICompatible

Documentation version: Edge

Canonical HTML: https://littleghostai.org/docs/LittleGhost/Providers/OpenAICompatible.html

OpenAICompatible brings OpenAI-style Responses or Chat Completions endpoints
into LittleGhost. Agents receive the same streaming events whether the
endpoint is OpenAI, a hosted model service, or an application gateway.

    provider = LittleGhost::Providers::OpenAICompatible.new(
      api_key: ENV.fetch("MODEL_API_KEY"),
      model: "example-model",
      base_url: "https://models.example.test/v1/"
    )

The client translates ModelRequest values to the selected wire API and
translates responses back to StreamEvent objects.

### Retries and streaming output

Transient HTTP and stream failures retry with limited exponential backoff. A
`:model_retry` event reports each retry and whether text had
already been emitted. Partial text may repeat after a retry, so consumers that
assemble streams must use that event to discard or replace superseded output.

## Inheritance

`LittleGhost::Providers::OpenAICompatible < LittleGhost::Providers::Base`

## Constants

### `DEFAULT_BASE_URL`

The OpenAI API endpoint used when `base_url` is omitted.

## Attributes

<a id="attribute-i-api"></a>
### `api` (R)

Provider model identifier and selected OpenAI-compatible wire API.

<a id="attribute-i-model"></a>
### `model` (R)

Provider model identifier and selected OpenAI-compatible wire API.

## Class methods

<a id="method-c-new"></a>
### `.new`

```ruby
.new(api_key:,
        model:,
        base_url: DEFAULT_BASE_URL,
        api: :responses,
        headers: {},
        open_timeout: 10,
        read_timeout: 120,
        allow_insecure_http: false,
        max_response_bytes: Support::HTTPClient::DEFAULT_MAX_RESPONSE_BYTES,
        max_retries: 2,
        max_retry_delay: MAX_RETRY_DELAY,
        transport: nil,
        sleeper: nil,
        on_retry: ->(*) {})
```

Configures an OpenAI-compatible client.

`api` is `:responses` or `:chat_completions`. `headers`
adds trusted endpoint-specific headers. `max_retries` controls retries before
the original error is raised, and `on_retry` receives the attempt, error, and
delay. Pass a custom `transport` for alternate HTTP execution.

<a id="method-c-request_options"></a>
### `.request_options`

```ruby
.request_options()
```

Request policy supported by OpenAI-compatible HTTP clients.

## Instance methods

<a id="method-i-capabilities"></a>
### `#capabilities`

```ruby
#capabilities(metadata: {})
```

Returns the permissive capability contract expected from compatible APIs.
Subclasses can override this when the endpoint advertises precise support.

<a id="method-i-stream"></a>
### `#stream`

```ruby
#stream(request)
```

Streams LittleGhost StreamEvent objects for `request`.

Without a block, returns an Enumerator. Context-window errors normalize to
ContextWindowOverflowError, and malformed tool calls normalize to
MalformedToolCallError.
