Expose API in Laravel
LarAgent\API\Completions handles OpenAI compatible chat completion requests.
The class expects a valid Illuminate\Http\Request and an agent class name:
$response is either an array (For non-streaming responses) or a Generator with chunks (for streaming responses).
Base Controllers
To not bother you with building the controllers withCompletions class we create abstract classes,
So that you can use the provided base controllers to create endpoints quickly by extending them.
Both controllers implement a
completion(Request $request) method that delegates work to Completions::make()
and automatically handles SSE streaming or JSON responses compatible with OpenAI API.SingleAgentController
Simple controller for exposing a single agent providingcompletion and models methods.
Once you have your agent created, 3 steps is enough to expose it via API.
Extend SingleAgentController when exposing a single agent:
- Set
protected ?string $agentClassproperty to specify the agent class. - Set
protected ?array $modelsproperty to specify the models.
- Define the API routes in your Laravel application
MultiAgentController
When several agents share one endpoint extendMultiAgentController:
- Set
protected ?array $agentsproperty to specify the agent classes. - Set
protected ?array $modelsproperty to specify the models.
model as AgentName/model or as AgentName (Default model is used defined in Agent class or provider).
- Define the API routes in your Laravel application
Storing chat histories
Since the most of clients manage the chat history on their side, this method is not necessary if you don’t want to store chats. Without this method, the session id will be random string per each request, you can easily set “in_memory” as a chat history type of your exposed agent and forget about it. But if you want to store the chat histories and maintain the state on your side, you will need to set the session id for the agent usingsetSessionId method in SingleAgentController or MultiAgentController.
Streaming response
Streaming responses are sent as Server-Sent Events where each event contains a JSON chunk matching OpenAI’s streaming format. Including"stream": true in request returns a text/event-stream where each chunk matches the OpenAI format and includes:
Note that the
usage data is included only in the last chunk as in OpenAI API.Calling from a Custom Controller
If you need more control you may callCompletions::make() directly:

