Stateful GenServer that owns one conversation and runs the completion-tool-call loop.
Modes
manage_history: true(default) — Agent accumulates messages betweenprompt/2calls. The conversation grows automatically, like Laravel/AI's Agent.manage_history: false— Agent is a stateless runner. Consumer passesmessages:in eachprompt/3call and manages history externally.
Usage
{:ok, pid} = PhoenixAI.Agent.start_link(
provider: :openai,
model: "gpt-4o",
system: "You are a helpful assistant.",
tools: [MyApp.Weather],
api_key: "sk-..."
)
{:ok, response} = PhoenixAI.Agent.prompt(pid, "What's the weather in Lisbon?")
response.content
#=> "The weather in Lisbon is sunny, 22°C!"
{:ok, response} = PhoenixAI.Agent.prompt(pid, "And in Porto?")Supervision
Start under a DynamicSupervisor:
DynamicSupervisor.start_child(MyApp.AgentSupervisor, {PhoenixAI.Agent, opts})
Summary
Functions
Returns a specification to start this module under a supervisor.
Returns the accumulated conversation messages.
Sends a prompt to the agent and waits for the response.
Clears conversation history, keeps configuration. Returns {:error, :agent_busy} if a prompt is in flight.
Starts an Agent GenServer. See module docs for options.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_messages(GenServer.server()) :: [PhoenixAI.Message.t()]
Returns the accumulated conversation messages.
@spec prompt(GenServer.server(), String.t(), keyword()) :: {:ok, PhoenixAI.Response.t()} | {:error, term()}
Sends a prompt to the agent and waits for the response.
Blocks until the provider (and any tool calls) complete. Default timeout: 60 seconds.
Options (prompt/3)
:messages— override conversation history (formanage_history: false):timeout— override call timeout in milliseconds
@spec reset(GenServer.server()) :: :ok | {:error, :agent_busy}
Clears conversation history, keeps configuration. Returns {:error, :agent_busy} if a prompt is in flight.
@spec start_link(keyword()) :: GenServer.on_start()
Starts an Agent GenServer. See module docs for options.