# `Mojentic.Agents.IterativeProblemSolver`
[🔗](https://github.com/svetzal/mojentic-ex/blob/v1.5.0/lib/mojentic/agents/iterative_problem_solver.ex#L1)

An agent that iteratively attempts to solve a problem using available tools.

This solver uses a chat-based approach to break down and solve complex problems.
It will continue attempting to solve the problem until it either succeeds,
fails explicitly, or reaches the maximum number of iterations.

The solver uses the ChatSession to maintain conversation state and automatically
handles tool calls through the broker. It monitors the LLM's responses for
completion indicators ("DONE" or "FAIL") to determine when to stop iterating.

## Usage

    alias Mojentic.LLM.Broker
    alias Mojentic.LLM.Gateways.Ollama
    alias Mojentic.LLM.Tools.{DateResolver, AskUser}
    alias Mojentic.Agents.IterativeProblemSolver

    broker = Broker.new("qwen3:32b", Ollama)

    solver = IterativeProblemSolver.new(
      broker,
      tools: [DateResolver, AskUser],
      max_iterations: 5
    )

    case IterativeProblemSolver.solve(solver, "What's the date next Friday?") do
      {:ok, result} -> IO.puts("Result: #{result}")
      {:error, reason} -> IO.puts("Error: #{inspect(reason)}")
    end

## Options

- `:tools` - List of tool modules available to the LLM (default: [])
- `:max_iterations` - Maximum number of iterations before giving up (default: 3)
- `:system_prompt` - Custom system prompt (default: problem-solving assistant prompt)
- `:temperature` - LLM temperature for response generation (default: 1.0)

## Completion Indicators

The solver monitors the LLM's responses for these keywords:
- "DONE" (case-insensitive) - Task completed successfully
- "FAIL" (case-insensitive) - Task cannot be completed

When either indicator is detected, the solver requests a final summary
and returns that to the caller.

# `t`

```elixir
@type t() :: %Mojentic.Agents.IterativeProblemSolver{
  broker: Mojentic.LLM.Broker.t(),
  max_iterations: pos_integer(),
  system_prompt: String.t(),
  temperature: float(),
  tools: [module()]
}
```

# `new`

```elixir
@spec new(
  Mojentic.LLM.Broker.t(),
  keyword()
) :: t()
```

Creates a new IterativeProblemSolver.

## Parameters

- `broker` - The LLM broker to use for generating responses
- `opts` - Keyword list of options:
  - `:tools` - List of tool modules (default: [])
  - `:max_iterations` - Maximum iterations (default: 3)
  - `:system_prompt` - Custom system prompt (default: problem-solving prompt)
  - `:temperature` - LLM temperature (default: 1.0)

## Examples

    broker = Broker.new("qwen3:32b", Ollama)

    # With defaults
    solver = IterativeProblemSolver.new(broker)

    # With custom options
    solver = IterativeProblemSolver.new(broker,
      tools: [MyTool],
      max_iterations: 5,
      system_prompt: "You are a specialized assistant.",
      temperature: 0.7
    )

# `solve`

```elixir
@spec solve(t(), String.t()) :: {:ok, String.t()} | {:error, term()}
```

Executes the problem-solving process.

This method runs the iterative problem-solving process, continuing until one of
these conditions is met:
- The task is completed successfully ("DONE")
- The task fails explicitly ("FAIL")
- The maximum number of iterations is reached

After the loop completes, the solver requests a summary of the final result,
excluding process details.

## Parameters

- `solver` - The IterativeProblemSolver instance
- `problem` - The problem or request to be solved

## Returns

- `{:ok, summary}` - Success with final result summary
- `{:error, reason}` - Error from broker or chat session

## Examples

    {:ok, result} = IterativeProblemSolver.solve(solver, "Calculate the area of a circle with radius 5")
    # => {:ok, "The area is approximately 78.54 square units."}

    {:ok, result} = IterativeProblemSolver.solve(solver, "What's the weather tomorrow?")
    # Uses tools iteratively to gather info and answer

---

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