# `ElixirLLM.Providers.Base`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L1)

Base functionality shared across all LLM providers.

This module extracts common patterns to eliminate duplication across provider
implementations. Providers can use these helpers directly or override as needed.

## Usage

    defmodule MyProvider do
      alias ElixirLLM.Providers.Base

      # Use shared helpers
      defp parse_error(status, body), do: Base.parse_error(status, body, :my_provider)
      defp accumulate_chunk(acc, chunk), do: Base.accumulate_chunk(acc, chunk)
    end

# `accumulate_chunk`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L44)

```elixir
@spec accumulate_chunk(map(), ElixirLLM.Chunk.t()) :: map()
```

Accumulates a chunk into the streaming state.

# `bearer_headers`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L379)

```elixir
@spec bearer_headers(String.t() | nil) :: [{String.t(), String.t()}]
```

Builds standard Bearer token headers.

Validates that the API key is present and raises a clear error if not.

# `build_final_response`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L68)

```elixir
@spec build_final_response(map()) :: ElixirLLM.Response.t()
```

Builds the final Response from accumulated streaming state.

# `create_ndjson_fold_fun`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L527)

```elixir
@spec create_ndjson_fold_fun((ElixirLLM.Chunk.t() -&gt; any()), (map() -&gt;
                                                          ElixirLLM.Chunk.t()
                                                          | nil)) ::
  (term(), {term(), term(), map()} -&gt; {:cont, {term(), term(), map()}})
```

Creates a streaming fold function for NDJSON format.
@deprecated Use create_ndjson_stream_fun/2 instead

# `create_ndjson_stream_fun`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L464)

```elixir
@spec create_ndjson_stream_fun((ElixirLLM.Chunk.t() -&gt; any()), (map() -&gt;
                                                            ElixirLLM.Chunk.t()
                                                            | nil)) ::
  ({:data, binary()}, {term(), term()} -&gt; {:cont, {term(), term()}})
```

Creates a streaming function for NDJSON format.

# `create_sse_fold_fun`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L513)

```elixir
@spec create_sse_fold_fun((ElixirLLM.Chunk.t() -&gt; any()), (map() -&gt;
                                                       ElixirLLM.Chunk.t() | nil)) ::
  (term(), {term(), term(), map()} -&gt; {:cont, {term(), term(), map()}})
```

Creates a streaming fold function that processes SSE chunks.
@deprecated Use create_sse_stream_fun/2 instead

# `create_sse_stream_fun`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L451)

```elixir
@spec create_sse_stream_fun((ElixirLLM.Chunk.t() -&gt; any()), (map() -&gt;
                                                         ElixirLLM.Chunk.t()
                                                         | nil)) ::
  ({:data, binary()}, {term(), term()} -&gt; {:cont, {term(), term()}})
```

Creates a streaming function that processes SSE chunks.

Uses process dictionary to accumulate state since Req 0.5.x `into:` option
only supports functions with `{:data, data}, {req, resp}` signature.

# `format_messages_openai`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L294)

```elixir
@spec format_messages_openai(list()) :: [map()]
```

Formats messages for OpenAI-compatible APIs.

# `format_parameters`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L261)

```elixir
@spec format_parameters(map()) :: map()
```

Formats tool parameters as JSON Schema.

# `format_tools_openai`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L231)

```elixir
@spec format_tools_openai(list()) :: [map()]
```

Formats tools for OpenAI-compatible APIs.

Most providers accept this format directly.

# `get_stream_accumulator`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L487)

```elixir
@spec get_stream_accumulator() :: map()
```

Gets and clears the stream accumulator from the process dictionary.
Returns the final accumulated state.

# `get_timeout`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L364)

```elixir
@spec get_timeout(atom()) :: pos_integer()
```

Gets the configured timeout for a provider.

# `init_stream_accumulator`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L477)

```elixir
@spec init_stream_accumulator() :: :ok
```

Initializes the stream accumulator in the process dictionary.
Must be called before starting a stream request.

# `initial_accumulator`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L29)

```elixir
@spec initial_accumulator() :: map()
```

Initial accumulator state for streaming responses.

# `maybe_add`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L343)

```elixir
@spec maybe_add(map(), atom(), term()) :: map()
```

Conditionally adds a key-value pair to a map if value is not nil.

# `maybe_add_tools`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L350)

```elixir
@spec maybe_add_tools(map(), list(), (list() -&gt; list())) :: map()
```

Adds tools to request body if any are registered.

# `merge_tool_calls`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L60)

```elixir
@spec merge_tool_calls(list(), list() | nil) :: list()
```

Merges tool calls from streaming chunks.

# `parse_chat_response`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L554)

```elixir
@spec parse_chat_response(map(), (list() | nil -&gt; [ElixirLLM.ToolCall.t()] | nil)) ::
  ElixirLLM.Response.t()
```

Parses a standard OpenAI-compatible chat completion response.

# `parse_error`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L146)

```elixir
@spec parse_error(integer(), map() | term(), atom()) :: struct()
```

Parses an error response and returns a structured error.

This converts provider-specific error responses into ElixirLLM's structured
error types for consistent error handling.

# `parse_finish_reason`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L172)

```elixir
@spec parse_finish_reason(String.t() | nil) :: atom() | nil
```

Parses finish reason strings into atoms.

# `parse_ndjson_data`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L110)

```elixir
@spec parse_ndjson_data(String.t()) :: [map()]
```

Parses newline-delimited JSON (NDJSON) format.

Used by Ollama and some other providers that don't use SSE.

# `parse_sse_data`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L94)

```elixir
@spec parse_sse_data(String.t()) :: [map()]
```

Parses Server-Sent Events (SSE) data format.

Handles the standard `data: {...}` format used by OpenAI, Anthropic, and most providers.

# `parse_stream_chunk`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L574)

```elixir
@spec parse_stream_chunk(map(), (list() | nil -&gt; [ElixirLLM.ToolCall.t()] | nil)) ::
  ElixirLLM.Chunk.t() | nil
```

Parses a standard OpenAI-compatible streaming chunk.

# `parse_tool_calls`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L195)

```elixir
@spec parse_tool_calls(list() | nil) :: [ElixirLLM.ToolCall.t()] | nil
```

Parses tool calls from OpenAI-compatible format.

Most providers (OpenAI, Groq, Mistral, Together, OpenRouter) use this format.

# `require_api_key!`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/providers/base.ex#L415)

```elixir
@spec require_api_key!(atom()) :: String.t()
```

Validates that an API key is configured and returns it.

Raises ArgumentError with a helpful message if the key is missing.

---

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