# `LangChain.Callbacks`
[🔗](https://github.com/brainlid/langchain/blob/v0.13.0/lib/callbacks.ex#L1)

Defines the structure of callbacks and provides utilities for executing them.

See `LangChain.Chains.ChainCallbacks` for the list of callbacks that can be
used.

# `fire`
[🔗](https://github.com/brainlid/langchain/blob/v0.13.0/lib/callbacks.ex#L16)

```elixir
@spec fire([map()], atom(), [any()]) :: :ok | no_return()
```

Fire a named callback with the list of arguments to pass. Takes a list of
callback handlers and will execute the callback for each handler that defines
a handler function for it.

# `reduce`
[🔗](https://github.com/brainlid/langchain/blob/v0.13.0/lib/callbacks.ex#L64)

```elixir
@spec reduce([map()], atom(), acc, (([any()] -&gt; any()), acc -&gt;
                                {:cont, acc} | {:halt, acc})) :: acc
when acc: term()
```

Fold a decision-returning callback across the attached handler maps.

Where `fire/3` discards what a handler returns, this collects it. A handler
map that does not define `callback_name` is skipped without consulting the
reducer.

`reducer` receives two arguments: an `invoke` function that applies the handler
to a list of arguments, and the current accumulator. It returns `{:cont, acc}`
to consult the next handler or `{:halt, acc}` to stop. Passing `invoke` rather
than the raw handler lets the reducer choose the arguments per handler, so a
handler can be shown what an earlier one changed, while the exception wrapping
stays here.

This is the primitive the module is built on. `fire/3` is this function with
the same arguments given to every handler and nothing accumulated.

## Example

    Callbacks.reduce(callbacks, :on_thing_reviewed, :allowed, fn invoke, acc ->
      case invoke.([subject]) do
        :ok -> {:cont, acc}
        {:denied, _reason} = denial -> {:halt, denial}
      end
    end)

---

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