# `ElixirLLM.Cache`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L1)

Response caching for LLM API calls.

Caches responses based on message content and model to reduce API costs
and improve response times for repeated queries.

## Configuration

    config :elixir_llm, :cache,
      enabled: true,
      ttl_ms: 3_600_000,  # 1 hour
      max_entries: 1000

## Example

    # Automatic caching with chat
    {:ok, response, chat} =
      ElixirLLM.new()
      |> ElixirLLM.with_cache()
      |> ElixirLLM.ask("What is 2+2?")

    # Direct cache operations
    cache_key = ElixirLLM.Cache.key(chat)
    {:ok, response} = ElixirLLM.Cache.get(cache_key)
    :ok = ElixirLLM.Cache.put(cache_key, response)

## Cache Key Generation

Cache keys are generated from:
  * Model ID
  * Message history (content only, not metadata)
  * Temperature setting
  * Tool definitions (if any)

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

```elixir
@type cache_key() :: binary()
```

# `child_spec`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L37)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `clear`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L143)

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

Clears all cached entries.

# `delete`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L133)

```elixir
@spec delete(cache_key()) :: :ok
```

Deletes a cached entry.

# `fetch`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L173)

```elixir
@spec fetch(cache_key(), (-&gt; {:ok, term()} | {:error, term()})) ::
  {:ok, term()} | {:error, term()}
```

Fetches a cached value or computes and caches it.

## Example

    ElixirLLM.Cache.fetch(cache_key, fn ->
      provider.chat(chat)
    end)

# `get`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L79)

```elixir
@spec get(cache_key()) :: {:ok, ElixirLLM.Response.t()} | :miss
```

Gets a cached response.

Returns `{:ok, response}` if found and not expired, `:miss` otherwise.

# `key`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L61)

```elixir
@spec key(ElixirLLM.Chat.t()) :: cache_key()
```

Generates a cache key for the given chat.

# `put`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L112)

```elixir
@spec put(cache_key(), ElixirLLM.Response.t()) :: :ok
```

Caches a response.

# `size`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L157)

```elixir
@spec size() :: non_neg_integer()
```

Returns the number of cached entries.

# `start_link`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/cache.ex#L53)

Starts the cache.

---

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