PHPackages                             shawnveltman/laravel-openai - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. shawnveltman/laravel-openai

ActiveLibrary[API Development](/categories/api)

shawnveltman/laravel-openai
===========================

An OpenAI API wrapper that uses the Laravel Http facade for easier testing

20231126(2y ago)01.6k1[3 PRs](https://github.com/shawnveltman/laravel-openai/pulls)2MITPHPPHP ^8.1CI passing

Since Nov 26Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/shawnveltman/laravel-openai)[ Packagist](https://packagist.org/packages/shawnveltman/laravel-openai)[ Docs](https://github.com/shawnveltman/laravel-openai)[ RSS](/packages/shawnveltman-laravel-openai/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (15)Versions (61)Used By (2)

Laravel OpenAI - Multi-Provider AI API Wrapper
==============================================

[](#laravel-openai---multi-provider-ai-api-wrapper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2a1bc0e985767bd3e3b2495d0cf9c2713ed7b7d6ddce4f274bce157831f7fb3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736861776e76656c746d616e2f6c61726176656c2d6f70656e61692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/shawnveltman/laravel-openai)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f7ce1b17f1bf959de374f7cd86fa70b6cf1c5d9617351b96ae67bca69f771a6e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736861776e76656c746d616e2f6c61726176656c2d6f70656e61692f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/shawnveltman/laravel-openai/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/652496ec1833dc51ca27f16eb08f594a07b5237e6166a8578d017f215dad3f0b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736861776e76656c746d616e2f6c61726176656c2d6f70656e61692f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/shawnveltman/laravel-openai/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/fc52352d4a14f7417a5227da3fa502c651b599a9337c82bff96a259f8c6a918e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736861776e76656c746d616e2f6c61726176656c2d6f70656e61692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/shawnveltman/laravel-openai)

A unified Laravel package for interacting with multiple AI provider APIs (OpenAI, Claude/Anthropic, Gemini, and Mistral). Uses Laravel's HTTP facade for seamless testing and mocking, with built-in cost tracking and support for advanced features like extended thinking and reasoning.

Features
--------

[](#features)

- 🤖 **Multi-Provider Support**: OpenAI, Claude/Anthropic, Gemini, and Mistral
- 🧪 **Easy Testing**: Built on Laravel's HTTP facade for simple mocking
- 💰 **Cost Tracking**: Automatic logging of API usage and costs
- 🧠 **Extended Thinking**: Support for Claude's extended thinking and OpenAI's reasoning effort
- 🎯 **Unified Interface**: Consistent API across all providers
- 🖼️ **Image Support**: Send images to vision-capable models
- 🔄 **Retry Logic**: Automatic handling of rate limits and token limits

Installation
------------

[](#installation)

Install via Composer:

```
composer require shawnveltman/laravel-openai
```

Publish and run migrations for cost tracking:

```
php artisan vendor:publish --tag="laravel-openai-migrations"
php artisan migrate
```

Publish the config file:

```
php artisan vendor:publish --tag="laravel-openai-config"
```

Add your API keys to `.env`:

```
OPEN_API_KEY=your-openai-key
ANTHROPIC_API_KEY=your-anthropic-key
GEMINI_API_KEY=your-gemini-key
MISTRAL_API_KEY=your-mistral-key
```

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

```
use Shawnveltman\LaravelOpenai\ProviderResponseTestClass;

$ai = new ProviderResponseTestClass();

// Works with any supported model
$response = $ai->get_response_from_provider(
    prompt: 'Explain quantum computing in simple terms',
    model: 'gpt-5-nano', // or 'claude-sonnet-4', 'gemini-pro', etc.
);
```

### Using Thinking Effort

[](#using-thinking-effort)

Control the reasoning depth with the `ThinkingEffort` enum:

```
use Shawnveltman\LaravelOpenai\Enums\ThinkingEffort;

// Minimal thinking for simple tasks (fast, cheaper)
$response = $ai->get_response_from_provider(
    prompt: 'Format this data as JSON',
    model: 'gpt-5-nano',
    thinking_effort: ThinkingEffort::MINIMAL
);

// High thinking for complex reasoning (slower, more thorough)
$response = $ai->get_response_from_provider(
    prompt: 'Solve this complex mathematical proof',
    model: 'claude-sonnet-4',
    thinking_effort: ThinkingEffort::HIGH
);
```

**Thinking Effort Levels:**

- `MINIMAL`: Fast responses, minimal reasoning (0 tokens for Claude, "minimal" for OpenAI)
- `LOW`: Basic reasoning (6,000 tokens / "low")
- `MEDIUM`: Standard reasoning - default (16,000 tokens / "medium")
- `HIGH`: Deep reasoning (25,000 tokens / "high")

### JSON Mode

[](#json-mode)

Force responses in JSON format:

```
$response = $ai->get_response_from_provider(
    prompt: 'List the top 5 programming languages with descriptions',
    model: 'gpt-5-nano',
    json_mode: true
);
```

### Working with Images

[](#working-with-images)

```
$response = $ai->get_response_from_provider(
    prompt: 'What is in this image?',
    model: 'gpt-4o',
    image_urls: ['https://example.com/image.jpg']
);
```

### System Prompts and Messages

[](#system-prompts-and-messages)

```
$response = $ai->get_response_from_provider(
    prompt: 'Write a haiku',
    model: 'claude-sonnet-4',
    system_prompt: 'You are a professional poet',
    messages: [
        ['role' => 'user', 'content' => 'Previous message'],
        ['role' => 'assistant', 'content' => 'Previous response'],
    ]
);
```

### Advanced Configuration

[](#advanced-configuration)

```
$response = $ai->get_response_from_provider(
    prompt: 'Your prompt here',
    model: 'claude-sonnet-4',
    user_id: 123,
    description: 'Marketing copy generation',
    job_uuid: 'unique-job-id',
    temperature: 0.7,
    max_tokens: 4096,
    thinking_effort: ThinkingEffort::MEDIUM
);
```

Supported Models
----------------

[](#supported-models)

### OpenAI

[](#openai)

- GPT-5 series: `gpt-5`, `gpt-5-mini`, `gpt-5-nano`
- GPT-4 series: `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`
- O-series: `o1`, `o1-mini`, `o3`, `o3-mini`, `o4-mini`

### Anthropic Claude

[](#anthropic-claude)

- `claude-sonnet-4-20250514` (default)
- `claude-opus-4`
- Other Claude models

### Google Gemini

[](#google-gemini)

- `gemini-pro`
- `gemini-1.5-pro`

### Mistral

[](#mistral)

- `mistral-large`
- `mistral-medium`
- `codestral`

Cost Tracking
-------------

[](#cost-tracking)

The package automatically logs all API calls to the `cost_logs` table:

```
use Shawnveltman\LaravelOpenai\Models\CostLog;

// Get costs for a user
$costs = CostLog::where('user_id', 123)->get();

// Get costs by model
$costs = CostLog::where('model', 'gpt-5-nano')->sum('input_tokens');
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Shawn Veltman](https://github.com/shawnveltman)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance58

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 89.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

55

Last Release

175d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6dd8d72a34070f86fc3cd5180b2cacbf81d41d69b1016b91da26b5ef409b577c?d=identicon)[shawnveltman](/maintainers/shawnveltman)

---

Top Contributors

[![shawnveltman](https://avatars.githubusercontent.com/u/8326484?v=4)](https://github.com/shawnveltman "shawnveltman (194 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (8 commits)")[![WillJacksonHale](https://avatars.githubusercontent.com/u/122298943?v=4)](https://github.com/WillJacksonHale "WillJacksonHale (4 commits)")

---

Tags

laravellaravel-openaishawnveltman

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/shawnveltman-laravel-openai/health.svg)

```
[![Health](https://phpackages.com/badges/shawnveltman-laravel-openai/health.svg)](https://phpackages.com/packages/shawnveltman-laravel-openai)
```

###  Alternatives

[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[njoguamos/laravel-plausible

A laravel package for interacting with plausible analytics api.

208.8k](/packages/njoguamos-laravel-plausible)[ryangjchandler/bearer

Minimalistic token-based authentication for Laravel API endpoints.

8129.8k](/packages/ryangjchandler-bearer)[codebar-ag/laravel-docuware

DocuWare integration with Laravel

1221.1k](/packages/codebar-ag-laravel-docuware)[combindma/laravel-facebook-pixel

Meta pixel integration for Laravel

4956.9k](/packages/combindma-laravel-facebook-pixel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
