Async

Run workflows in an async context.

Neuron supports asynchronous execution and parallel processing of agents, enabling you to efficiently handle multiple operations simultaneously. Neuron's approach to asynchronous execution offers several advantages:

Framework Agnostic: You can make agents and RAG async friendly for the most common async environments with a simple adapter, no changes are required to your implementation.

Providers Compatibility: Our solution works regardless of the provider you use. You can have workflow or multi-agent systems using different providers and models running smoothly in an async loop.

Scalability: Applications handling large volumes of data (products classification, content moderation, data labeling) benefit significantly from concurrent processing capabilities.

Concurrency vs Async

Concurrency is the high-level concept of managing tasks, which can involve multiple threads/cores (parallelism), whereas async uses event loops/callbacks to let tasks run out of order, perfect for I/O-bound work without waiting.

Concurrency is about managing many things in parallel, while asynchrony is how a single thread can manage many I/O operations efficiently.

AI Agents are typically considered I/O heavy software because the HTTP request to run inference on the model usually takes seconds to complete. In a standard PHP enviornment during this time your application just wait. In this section of the documentation we provide you with a couple of solutions to run multiple agent efficiently.

Concurrency

You don't need any particular feature from Neuron to run multiple agents in parallel. You only need your PHP application to be able to span multiple processes to handle the execution of multiple agents at the same time. You can do this with PHP libraries like spatie/forkarrow-up-right, or framework specific solutions like Laravel concurrencyarrow-up-right, or Symfony processarrow-up-right.

Async is a different story.

Async

Previous versions of Neuron were strongly coupled with the Guzzle client to perform HTTP requests for model inference on the providers API. Guzzle is a great tool, but it's not compatible with truly async event loops like those provided by frameworks like Amparrow-up-right and ReactPHParrow-up-right.

In order to run agents in such async environments it's required to integrate with their specific implementations. That's why Neuron ships with a simple HttpClientInterface that can be implemented to allow AI providers run HTTP requests smoothly in an async loop.

By default the framework uses the Guzzle implementation, but you can inject custom HTTP clients based on your needs. We already provide implementations for the most common async framework.

AmpHttpClient

If you want to use Amp to run multiple async agent requests you need to install amphp/http-client .

Now you can inject the built-in AmpHttpClient adapter into the provider:

Now you can run multiple agent requests using Amp async/await pattern to run multiple agent asynchronously:

Async RAG

The HTTP client abstraction is also accepted by all the other framework components, like embedding providers and vector stores. You can provide an async client to all this components and also run data loading pipeline asynchronously.

Guardrails

The ability to execute agents asynchonously opens the possibility of running an input guardrail at the same time as the main request. Typically you can create a specialized agent to run security checks against the user input, and use the structured reponse to retrieve the result.

You can run both requests in parallel, and check the guardrail result before returning the response to the user. Running both requests in parallel allows you to enforce security without impacting the user experience.

Last updated