Middleware

Interact with the agent execution flow to customize its behaviour.

Middleware is a feature of the basic Workflow component, it's not specific to agents. This means you are free to attach custom middleware to Agent, RAG, and Workflows to hook into their execution cycle. In this section you can find a set of built-in middleware we provide for common use cases.

Summarization

Automatically summarize conversation history when approaching token limits.

use NeuronAI\Agent\Agent;
use NeuronAI\Agent\Middleware\Summarization;
use NeuronAI\Workflow\Middleware\WorkflowMiddleware;
use NeuronAI\Workflow\NodeInterface;

class MyAgent extends Agent
{
    protected function provider(): AIProviderInterface
    {...}

    protected function instructions(): string
    {...}

    /**
     * Define your middleware here.
     *
     * @return array<class-string<NodeInterface>, WorkflowMiddleware[]>
     */
    protected function middleware(): array
    {
        $summarization = new Summarization(
            provider: $this->resolveProvider(), // Or use a dedicated provider instance
            maxTokens: 30000,
            messagesToKeep: 10,
        );
        
        return [
            ChatNode::class => [$summarization],
            StreamingNode::class => [$summarization],
            StructuredOutputNode::class => [$summarization]
        ];
    }
}

maxTokens and messagesToKeep work together to define the threshold beyond which the summary is performed. In the example above, if the context reach 30K tokens, there must be at least 10 messages in the chat history for the summary to start. Adding new messages to the chat history will eventually cross both thresholds allowing the summarization.

Human In The Loop

In Neuron the Agent entity is built on top of the Workflow component. That means it can be interrupted to ask confirmation before performing critical actions. This middleware pause agent execution for human approval or rejection of tool calls before they execute.

use NeuronAI\Agent\Agent;
use NeuronAI\Agent\Middleware\ToolApproval;
use NeuronAI\Workflow\Middleware\WorkflowMiddleware;
use NeuronAI\Workflow\NodeInterface;

class MyAgent extends Agent
{
    protected function provider(): AIProviderInterface
    {...}

    protected function instructions(): string
    {...}

    /**
     * Define your middleware here.
     *
     * @return array<class-string<NodeInterface>, WorkflowMiddleware[]>
     */
    protected function middleware(): array
    {
        return [
            ToolNode::class => [
                new ToolApproval(
                    // Provide a list of tool names that need to be approved
                    tools: ['sql_delete', 'file_delete']
                )
            ],
        ];
    }
}

When the agent recognizes that the model wants to invoke a sensitive function, an interrupt is triggered. You can catch this interruption and allow the user to provide a feedback (approve, reject). To better understand how to manage this interrutpion you can check out this example: v

Or refer to the full workflow documentation.

Last updated