# Add a function

> Gives an agent a fixed pipeline of steps it calls as one tool, for the part of its work that must come out the same every time.

**Skill for your coding agent:** [`mindset-function/SKILL.md`](https://docs.mindset.ai/skills/mindset-function/SKILL.md)

Building an agent goes in order: build the agent, connect your systems, add functions, scripts and widgets, test it, take it live, then put it in your product. Highlighted: Add functions, scripts and widgets.

## Ask your coding agent

```
Build a function that compares an invoice to its purchase order line by line and returns the differences, their total value and whether there are any. Test it on INV-4471 and on one invoice that matches, then give it to the invoice exceptions agent.
```

Skill: [mindset-function](https://docs.mindset.ai/skills/mindset-function/SKILL.md)

## What gets built

A [function](https://docs.mindset.ai/docs/functions): a fixed list of steps the agent calls as one tool. A step calls an operation, calls a model through an AI model [connection](https://docs.mindset.ai/docs/connection-types), works out a value, or chooses between values. Every step runs, and a function can't call another function. It's published as a version, and agents always use the published one.

## What you decide

- **Whether it's a function at all.** It gives the same answer for the same input. Comparing an invoice to its purchase order qualifies. Deciding whether a difference is acceptable under the contract is a judgment, and stays with the agent.
- **What it returns.** Return the value a [script](https://docs.mindset.ai/docs/write-a-script) decides on, such as whether there are any differences.
- **Its test parameters.** One set of inputs for each path, including one awkward case.
- **Whether it chooses by who the session is for.** To answer differently per plan, region or account, read the label your backend set as `attribution.<key>`, never an input a caller could send. Give each label value its own test case with `attribution` set, plus one with no labels at all.
- **Which failures to retry.** Retries are off unless you ask, up to 10 attempts in total.

A run an agent starts has a 300-second deadline and at most 1,000 outside calls. [Limits](https://docs.mindset.ai/docs/limits) has the rest.

> **Warning:** Every operation the function calls must be marked **Available to functions** on its connection, and tested, or publishing stops.

## Done when

- Its test parameters read **Verified**: every step ran.
- It's published, and an active agent version holds it. Publishing is refused while it calls an untested operation, unless an admin chooses **Go live untested**.
- The agent called it in a conversation and used what it returned.

## See it working

**Live demo:** [Ask your data](https://docs.mindset.ai/demos/ask-your-data). Ask sales questions and a dashboard draws the answers from what the agent’s functions return.

**Live demo:** [Store assistant](https://docs.mindset.ai/demos/store). The agent filters an outdoor store’s product grid, points at its pick and fills your basket.

## In your repo

Each function is a file in the agent's `functions/` folder, holding the exact input `create_resource` takes. Your coding agent publishes a change once its test parameters pass. See [Keep your agents in your code](https://docs.mindset.ai/docs/keep-your-agents-in-your-code).

## In the Studio

**Functions → New function** opens the **Function Builder**. Describe the function and it writes the steps, which you can't edit by hand. **Preview → Document** writes the function out as prose, and **Preview → Diagram** runs it on **Fixture input**. Press **Save changes**, publish it on the **Versions** tab, then add it on the agent's **Resources** tab.

![A function's Preview tab, with the fixture input above the diagram of its steps.](https://docs.mindset.ai/images/docs/studio/function-preview.png)
