> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tryhamsa.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Static and local variables

> Set a variable to a fixed value or a Jinja formula when the call reaches a step, and keep derived values up to date with Keep in sync.

A local variable is a value you set yourself in a flow step, rather than one the caller gives or an API returns. Use it to record where the call went (`route = "billing"`), to fix a value for later steps, or to work out a new value from others, like a spoken date from an ISO date. The UI calls them **Local variables**; their values can be static text or [Jinja](https://docs.tryhamsa.com/agents/variables/jinja.md).

Local variables are for [flow agents](https://docs.tryhamsa.com/agents/flow/overview.md).

## Where you can set them

| Where | When the value is set |
| - | - |
| A step's **Variables** section, with **Add variable** > **Local variable** | When the call reaches the step |
| The **Local variables** section of a [Transfer call](https://docs.tryhamsa.com/agents/flow/steps/transfer-call.md), [End call](https://docs.tryhamsa.com/agents/flow/steps/end-call.md), [Router](https://docs.tryhamsa.com/agents/flow/steps/router.md) or [Transfer to agent](https://docs.tryhamsa.com/agents/flow/steps/transfer-agent.md) step | When the call reaches the step |
| A **Set local variables** step | When the call reaches it. "This node sets local variables and automatically advances to the next node." See [Set variables step](https://docs.tryhamsa.com/agents/flow/steps/set-variables.md). |

## Set a local variable

**1. Add the variable**

In the step, add a local variable: **Add variable** > **Local variable** in the **Variables** section, or **Add variable** under **Local variables**.

**2. Name it**

In **Name**, type a snake\_case name, for example `route`.

**3. Pick a type**

Choose a **Type**: **String**, **Number**, **Boolean**, **Array (JSON)** or **Object (JSON)**.

**4. Set the value**

In **Value**, type the value. It can include variables and Jinja, for example `{{ customer_name | title }}`. "Leave empty to clear the variable's value."

**5. Optional: describe it**

Add a **Description** so your team knows what the variable is for.

![A Set local variables step, Record the route: below the route variable, spoken\_date is a String set to a Jinja formula with Keep in sync on](https://mintcdn.com/hamsa-452fa79a/7GY0-8K2sfzXu6G7/images/agents/variables/set-local-variables-light.webp?fit=max\&auto=format\&n=7GY0-8K2sfzXu6G7\&q=85\&s=6d1de1caf43f9b229424654095881de5)

From the next step on, use the variable like any other: `{{route}}`.

## Keep a value in sync

By default a local variable is worked out once: "Set once, as the call reaches this step." Turn on **Keep in sync** to recompute it from the other variables every time it's used: "Worked out again from the other variables each time it's used." The variable shows an **In sync** badge.

Use it for a value that depends on others that can still change, so it never goes stale:

```jinja theme={null}
{# delivery_note, kept in sync #}
{% if delivery_date %}Your order arrives on {{ delivery_date }}.{% else %}We'll confirm the delivery date soon.{% endif %}
```

Rules:

* **Keep in sync** needs a value with Jinja in it. Plain text has "Nothing to keep in sync". It isn't offered for the **Boolean** type.
* A kept-in-sync variable can read other kept-in-sync variables, so you can chain them (a date, then its ISO form, then a spoken form).
* A value that reads itself, like `{{ attempts | int + 1 }}` for `attempts`, can't be kept in sync: "It reads itself, so the call sets it once instead." The same applies to a loop through other variables.
* If anything later writes a plain value to the same name, that value replaces the formula and the variable stops recomputing.

## How it works on a call

* A set-once value is filled in when the call reaches the step, using the values known at that moment. Variables that have no value yet render empty.
* A kept-in-sync value is worked out each time a prompt, message or tool reads it. If working it out fails, the last good value is used.
* A kept-in-sync variable outranks other variables with the same name, except environment variables. See [Variables overview](https://docs.tryhamsa.com/agents/variables/overview.md#which-value-wins-when-names-clash).
* Every value is stored as text, whatever the **Type**. An array or object value written as JSON can still be used as a list or object in Jinja.

## Good to know

* To count something, set it once in a step that the call passes through each time: `{{ attempts | int + 1 }}` reads the old value and writes the new one.
* A local variable keeps its value for the rest of the call, also after the call leaves the step, until something writes the same name again.
* Give local variables names that don't match system variables. A local variable called `current_date` replaces the system value.

## Related

> [card] [Set variables step](https://docs.tryhamsa.com/agents/flow/steps/set-variables.md)
>
> A step that only sets variables, then moves on.

> [card] [Extracted variables](https://docs.tryhamsa.com/agents/variables/extracted-variables.md)
>
> Values the agent collects from the caller and from tools.

> [card] [Jinja syntax](https://docs.tryhamsa.com/agents/variables/jinja.md)
>
> Formulas, conditions and filters.

> [card] [Router step](https://docs.tryhamsa.com/agents/flow/steps/router.md)
>
> Branch on the values you set.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.