About 10 min.
Build an agentStart here
Set up your project
Create a Laravel project, add your OpenAI key, and make the folders the agent will live in.
2 min readLesson
You'll learn
- Create a fresh Laravel app to build in
- Store a secret API key safely in
.env - Read config values with
config()
What you need
- PHP 8.2 or newer, and Composer. Check by running
php -vandcomposer -V. - An OpenAI API key. Create one at platform.openai.com → API keys. Each request costs a little money; the “nano” models cost a fraction of a cent.
- A terminal and a code editor. Everything in this course runs as
php artisan …commands. There is no web page to build.
Step by step
1Create the Laravel project
Open a terminal in the folder where you keep your projects. Run these two commands to create a new app and move into it.
Run
composer create-project laravel/laravel leo-the-agent
cd leo-the-agent
2Make sure Laravel Prompts is installed
Laravel Prompts gives you the text() input box and the spin() loading spinner. New Laravel apps include it already, so this usually prints “Nothing to install”. That is fine.
Run
composer require laravel/prompts
3Add your API key to .env
Open the .env file in the project root. Scroll to the very bottom and paste these two lines. Replace sk-your-key-here with your real key.
Edit · .env
OPENAI_API_KEY=sk-your-key-here
OPENAI_MODEL=gpt-5.4-nano
Keep the key secret. .env is already in .gitignore, so it will not be committed. If your account uses a different model name, change OPENAI_MODEL.
4Expose the key through config
Open config/services.php. Find the closing ]; at the very end of the file. Paste this block just above it, so it sits inside the array.
Edit · config/services.php
'openai' => [
'key' => env('OPENAI_API_KEY'),
'model' => env('OPENAI_MODEL', 'gpt-5.4-nano'),
],
Your code will now read the key with config('services.openai.key') and the model with config('services.openai.model').
5Create the folders
All the AI code lives under app/Ai. Create the folders now so later lessons can drop files straight in.
Run
mkdir -p app/Ai/Agents app/Ai/Tools/Concerns app/Ai/Attributes app/Ai/Memory
Check your work
Run
Run
php artisan tinker --execute="dump(config('services.openai.key'))"
You should see
Your key printed in quotes, starting with "sk-. If you see null, the key is not being read: check the spelling in .env and config/services.php, then run php artisan config:clear.
