> For the complete documentation index, see [llms.txt](https://docs.neuron-ai.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.neuron-ai.dev/neuron-v4/workflow/persistence.md).

# Persistence

When we talk about persistence in Neuron, we're talking about the system's ability to capture and preserve the complete state of a running workflow at any moment.

Think of it like a sophisticated "save game" feature, but for business processes. At any point, when an interruption is asked from a node, Neuron create a snapshot of your workflow's state and store it in the persistence layer. Later, whether that's seconds, hours, or weeks, the workflow can be restored to exactly where it left of and continue as if nothing happened.

As usual in Neuron the Workflow persistence layer is built on top of a common interface so it's extensible and interchangeable. Below the supported persistence layer.

### InMemoryPersistence

It keep data in memory only for the current execution cycle.

```php
use NeuronAI\Workflow\Persistence\InMemoryPersistence;
use NeuronAI\Workflow\Persistence\PersistenceInterface;

class MyWorkflow extends Workflow
{
    ...
    
    protected function persistence(): PersistenceInterface
    {
        return new InMemoryPersistence();
    }
}
```

### FilePersistence

It will store the Workflow data and state into a local file.

```php
use NeuronAI\Workflow\Persistence\FilePersistence;
use NeuronAI\Workflow\Persistence\PersistenceInterface;

class MyWorkflow extends Workflow
{
    ...
    
    protected function persistence(): PersistenceInterface
    {
        return new FilePersistence(__DIR__);
    }
}
```

### DatabasePersistence

To persist the workflow interruption in the database you need to pass a `PDO` instance. If you are working on top of a framework you can easily get it from the ORM in the same way of the [SQLChatHistory](/neuron-v4/agent/chat-history-and-memory.md#sqlchathistory) for chat history persistence.

```php
use NeuronAI\Workflow\Persistence\DatabasePersistence;
use NeuronAI\Workflow\Persistence\PersistenceInterface;

class MyWorkflow extends Workflow
{
    ...
    
    protected function persistence(): PersistenceInterface
    {
        return new DatabasePersistence(
            pdo: new \PDO(...),
            table: 'workflow_interrupts'
        );
    }
}
```

Here are the SQL scripts to create the table:

{% tabs %}
{% tab title="MySQL/MariaDB" %}

```sql
CREATE TABLE workflow_steps (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    run_id VARCHAR(255) NOT NULL,
    step_id VARCHAR(255) NOT NULL,
    result LONGTEXT NOT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY workflow_steps_run_step_unique (run_id, step_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

{% endtab %}

{% tab title="PostgreSQL" %}

```sql
CREATE TABLE workflow_steps (
    id BIGSERIAL PRIMARY KEY,
    run_id VARCHAR(255) NOT NULL,
    step_id VARCHAR(255) NOT NULL,
    result TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL,
    CONSTRAINT workflow_steps_run_step_unique UNIQUE (run_id, step_id)
);
```

{% endtab %}

{% tab title="SQLite" %}

```sql
CREATE TABLE workflow_steps (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    run_id TEXT NOT NULL,
    step_id TEXT NOT NULL,
    result TEXT NOT NULL,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    UNIQUE (run_id, step_id)
);
```

{% endtab %}
{% endtabs %}

### EloquentPersistence

You should create your own Eloquent model and pass the class string as the constructor argument. The model can have custom relations, scopes, attributes, etc. but the basic structure must be based on this migration script:

```bash
php artisan make:migration create_workflow_interrupts_table --create=workflow_interrupts
```

```php
Schema::create('workflow_steps', function (Blueprint $table) {
    $table->id();
    $table->string('run_id');
    $table->string('step_id');
    $table->longText('result');
    $table->timestamps();
    
    $table->unique(['run_id', 'step_id']);
});
```

#### WorkflowInterrupt model

This is the minimal required structure:

```php
class WorkflowStep extends Model
{
    protected $fillable = ['run_id', 'step_id', 'result'];
}
```

Use it in the Workflow:

```php
use App\Models\WorkflowInterrupt;
use NeuronAI\Workflow\Persistence\EloquentPersistence;
use NeuronAI\Workflow\Persistence\PersistenceInterface;

class MyWorkflow extends Workflow
{
    ...
    
    protected function persistence(): PersistenceInterface
    {
        return new EloquentPersistence(WorkflowInterrupt::class);
    }
}
```
