Building AI-Driven Features in Symfony

· Silas Joisten · Expertise · 2 minutes to read
AI and Symfony

AI is transforming web development — and with php-llm/llm-chain, PHP developers can easily add powerful LLM features to Symfony apps. This guide shows you how to get started with chatbots, smart assistants, and more.

Why AI in Symfony?

AI is no longer a futuristic concept — it's part of today's tech stack. From chatbots to content enrichment and semantic search, AI-driven features are everywhere. Thanks to the php-llm/llm-chain library, integrating these capabilities into your Symfony project has never been easier.

Introduction

php-llm/llm-chain is a PHP-native library that allows you to interact with Large Language Models (LLMs) like:

It supports a wide variety of platforms (OpenAI, Azure, Replicate, etc.) and allows you to:

  • Generate content.

  • Call external tools (functions) with LLMs.

  • Embed and semantically search documents.

  • Chain multiple AI calls together with logic.

Symfony integration is provided via php-llm/llm-chain-bundle, which gives you automatic service registration, DI support, and config-driven setup.

Installing the Symfony Bundle

Install the package via Composer:

composer require php-llm/llm-chain-bundle

Configure your .env:

OPENAI_API_KEY=your-api-key-here

Configure the service in config/packages/llm_chain.yaml:

llm_chain:
  platform:
    openai:
      api_key: '%env(OPENAI_API_KEY)%'

  chain:
    default:
      model:
        name: 'gpt4o-mini'

Using AI in Your Symfony Service

Here’s a simple example of how to create a Symfony service that sends a message to an LLM and gets a response.

use PhpLlm\LlmChain\ChainInterface;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use PhpLlm\LlmChain\Model\Response\ResponseInterface;

final class SmartAssistant
{
    public function __construct(
        private ChainInterface $chain
    ) {
    }

    public function ask(string $question): ResponseInterface
    {
        $messages = new MessageBag(
            Message::forSystem('You are a helpful assistant.'),
            Message::ofUser($question),
        );

        return $this->chain->call($messages);
    }
}

You can now use this service in any controller, console command, or background worker.

Tool Calling: Make the AI Interactive

Want your LLM to call real PHP functions? Annotate them by using #[AsTool] attribute:

use PhpLlm\LlmChain\Toolbox\Attribute\AsTool;

#[AsTool('current_time', 'Returns the current server time')]
final class ClockTool
{
    public function __invoke(): string
    {
        return (new \DateTimeImmutable())->format('Y-m-d H:i:s');
    }
}

The LLM can now decide on its own when to use this function during a conversation. Think of it like ChatGPT Plugins… but in PHP.

llm-chain also supports embeddings for semantic search. You can store vectors in providers like:

This is great for implementing Retrieval-Augmented Generation (RAG) — a technique where you fetch contextually relevant documents before asking the LLM a question.

Try It: Symfony Demo Project

Want to test this out? The php-llm/llm-chain team provides a demo Symfony application showing chatbot interaction and vector search:

php-llm/llm-chain-symfony-demo

Controlling Costs and Tokens

LLMs aren’t free, so stay efficient with:

  • Cache repeating responses

  • Using short prompts

  • Monitoring token usage via logs

  • Limiting your system prompts

Conclusion

With just a few lines of configuration and code, you can integrate powerful AI features into your Symfony app. Whether you want to automate tasks, answer questions, or enrich content — llm-chain is a solid tool to get started.

Symfony is ready for the AI age. Are you?

Ready to Build Smarter Symfony Apps?

Start building smarter Symfony apps today with llm-chain — AI-powered features are just a few lines of code away.

This might also interest you

Illustration of Developer
Silas Joisten New

The Developer Experience Revolution 2026

Discover why Developer Experience matters more than ever and how better tools, smarter workflows, and a culture of learning can transform the way teams build software.

Read more
Nicolas Grekas with a mic in his right hand raising his left hand on stage at SymfonyCon Amsterdam 2025
Jules Daunay

Symfony 8: Stability, Security, and Innovation for Developers

To celebrate the launch of Symfony 8, we sat down with Nicolas Grekas, an emblematic figure in open-source and a major contributor to the framework. Between new JSON components, security hardening, and native integration with PHP 8.4, Nicolas explains why version 8 is a natural continuation of previous Symfony versions, without disrupting businesses. Read on for an overview to help you understand what's new and approach your upgrade with confidence.

Read more
Why PHP?
Silas Joisten

Why PHP Powers the Enterprise Web: The Strategic Advantage Companies Cannot Ignore

PHP remains one of the most reliable and cost effective backend technologies for enterprise systems.

Read more
Symfony UX training
Elise Hamimi

Boost Your Interfaces: Learn Symfony UX with the New Official Training by SensioLabs

In just a few years, Symfony UX has become a favorite among Symfony users. Perfectly aligned with modern  developers’ priorities, it allows you to easily build interactive, high-performance interfaces without leaving the comfort of the framework. It was time to bring this to our training catalog. That’s why we are proud to officially launch our new Symfony UX training program.

Read more
A yellow and white smiley on the ground with feets in sneakers
Rémi Brière

Giving Meaning to Agility with Evidence-Based Management

Agile frameworks, such as Scrum and Kanban, are well-known for helping teams focus more effectively on creating value and adapting to changes more easily. However, agility doesn't stop at the team level. Some approaches also address decision-makers and organizations. Evidence-Based Management is one such approach, offering a different perspective with its structured value and progress management based on concrete evidence.

Read more
Fabien Potencier
Elise Hamimi

SymfonyCon Amsterdam 2025: Our Recap and the Highlights

After an iconic first edition in 2019, SymfonyCon made its big comeback to Amsterdam. From the start, you could feel the energy of a highly anticipated conference: more than 1,200 attendees, 39 nationalities, the biggest Symfony community reunion of the year, great discoveries... and a fun atmosphere. This year was extra special because it was the 20th anniversary of Symfony. SensioLabs was there: we'll tell you all about our experience there!

Read more
Chart going up
Silas Joisten

Why Tests? Explained for Management

For business leaders: why testing matters for ROI, risk reduction, and agility explained in management language with numbers and real case studies.

Read more
Code happy in lights
Imen Ezzine

Code Review: Types, Organization, and Best Practices

Code review is an essential step in the software development cycle. It improves code quality, reduces bugs, and encourages knowledge sharing within the team. GitLab and GitHub, two of the most popular code management platforms, offer advanced features to facilitate this process. This article covers the various types of code reviews, how to organize them, and how to use templates and checklists to make PRs (pull requests) more efficient.

Read more
Image