# State Management - Standard Library Examples

> Demonstrates state operations in Tactus workflows. This example shows: - Initializing state with metatable syntax (`state.key = value`) - Using `State.increment()` for atomic counter operations - Reading state values for output and validation - Tracking workflow progress with state variables - Stat…

Canonical: https://tactus.anth.us/examples/standard-library/state-management/

::band{columns="document-hero wide"}

[Examples](/examples/) / [Standard Library](/examples/standard-library/) / State Management

# State Management

- Has Specs

Demonstrates state operations in Tactus workflows. This example shows: - Initializing state with metatable syntax (\`state.key = value\`) - Using \`State.increment()\` for atomic counter operations - Reading state values for output and validation - Tracking workflow progress with state variables - State assertions in specifications

::band{tone="muted" columns="example"}

## Source Code

```lua
-- State Management Example
-- Demonstrates setting, getting, and incrementing state values

-- No agents are needed for this example.

-- Procedure with outputs defined inline

Procedure {
    output = {
            success = field.boolean{required = true, description = "Whether the workflow completed successfully"},
            message = field.string{required = true, description = "Status message"},
            count = field.number{required = true, description = "Final count of processed items"},
    },
    function(input)

    Log.info("Starting state management example")

        -- Initialize state (metatable syntax)
        state.items_processed = 0

        -- Process items and track count
        for i = 1, 5 do
          State.increment("items_processed")
          Log.info("Processing item", {number = i})
        end

        -- Retrieve final state (metatable syntax)
        local final_count = state.items_processed
        Log.info("Completed processing", {total = final_count})

        return {
          success = true,
          message = "State management example completed successfully",
          count = final_count
        }

    -- BDD Specifications
    end
}

Specification([[
Feature: State Management
  Demonstrate state operations in Tactus workflows

  Scenario: State operations work correctly
    Given the procedure has started
    When the procedure runs
    Then the procedure should complete successfully
    And the state items_processed should be 5
    And the output success should be True
    And the output count should be 5
]])
```

## Quick Start

Run the example:

```bash
tactus run 03-standard-library/01-state-management.tac
```

Test with mocks:

```bash
tactus test 03-standard-library/01-state-management.tac --mock
```

[View source on GitHub →](https://github.com/AnthusAI/Tactus-examples/blob/main/03-standard-library/01-state-management.tac)

::band{columns="neighbors"}

-
- [*Next Example →* **File Io**](/examples/standard-library/file-io/)

::band{columns="cta"}

## Explore more examples

Learn Tactus through practical, runnable examples organized by topic.

::button{href="/examples/" label="View All Examples" size="lg"}
