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

# Code or prompt

> When a step of an agent's job should be a script, tool or workflow instead of an instruction to the model: fixed sequences, math, lookups, parsing, and anything that must run the same way every time. A rule of thumb, a good and a bad example, and where the code lives in Runtime. Read before writing a runbook step that counts, compares, converts, or always runs in the same order.

## Outcome

Every step of the job sits where it is cheapest to get right. The model does the judgment: reading the request, choosing what to look up, explaining the result. Code does the rest, so those steps give the same answer on every run, cost no tokens, and can be tested without an agent.

## The rule of thumb

**If you can write the step as a function with a correct answer, write the function.** Leave a step to the model only when it needs judgment or language: interpreting a messy request, deciding which case applies, writing for a person.

| Write code when the step... | Leave it to the model when the step... |
| - | - |
| Has one right answer (sums, dates, currency, proration, limits) | Has many acceptable answers (a reply, a summary) |
| Is a lookup (a code table, an ID mapping, a policy threshold) | Needs to interpret free text or an unfamiliar case |
| Always runs in the same order (fetch A, then B, then join) | Depends on what the last step found |
| Parses or reshapes data (CSV, JSON, logs, pagination) | Picks which data matters |
| Must never be skipped or reordered (a check before a write) | Can be adapted to the situation |

Two quick tests. Would you accept a wrong answer one run in fifty? If not, it is code. Would two good operators do this step differently? If yes, it is the model's.

## Example

The `payment-investigation` runbook needs the net amount the customer paid over 30 days.

| | Bad | Good |
| - | - | - |
| Runbook step | "List every charge and refund in the last 30 days, add up the charges, subtract the refunds and partial refunds, convert to the account currency, and state the net amount." | "Run `python scripts/timeline.py --customer <cus_id> --days 30`. It prints the timeline and `net_amount` as JSON. Quote `net_amount`; never recompute it." |
| What happens | Each run pages through Stripe a little differently, sometimes misses a partial refund, and does arithmetic in tokens. The grade passes on most runs and you cannot tell which ones are wrong. | The script pages, joins and sums the same way every time. The model spends its effort on what the timeline means for the customer. A wrong total is a bug you fix once in one file. |

The same split applies to the decline code table in `references/decline-codes.md`: a short table the model reads is fine, but once the mapping grows past a page or feeds a decision (refund or not), turn it into a lookup the script does.

## Where the code lives

Pick the narrowest home that does the job:

1. **A script in the skill.** Put it in the skill's `scripts/` folder and name the exact command in the runbook step. It ships with the skill to every template the skill is attached to. This covers most cases. See [Skills and runbooks](https://docs.runtm.com/build/how-it-works/skills.md).
2. **A CLI or MCP tool on the template.** When several skills or agents need the same operation, install it once as a tool provider package or expose it as an MCP tool. See [Give it tools](https://docs.runtm.com/build/give-it-tools.md).
3. **A hook.** When a check must run on every tool call, whatever the model decides (block writes to the ledger, require a ticket ID), write it as a guardrail hook. A runbook step can be skipped; a hook cannot. See [Guardrails and approvals](https://docs.runtm.com/build/guardrails-and-approvals.md).
4. **Outside the agent.** When the whole flow is fixed and only one step needs judgment, run the flow in your own job and call a session for that step through the API or a scheduled agent. The agent then never sees the parts it cannot improve.

## Verify

* Run the script by hand on one seeded case and check its output before an agent ever calls it.
* In a test run, read the transcript: the agent should call the script, not redo its work in prose.
* The grade on the step's output stays the same across repeated runs of the same case. If it flips, something non-deterministic is still in the prompt.

## Gotchas

* **"Be careful with the math" is not a fix.** Stronger wording makes errors rarer, not impossible. Move the math into code.
* **The model recomputes anyway.** Say in the runbook that the script's output is final and must be quoted, not recalculated.
* **Code that hides judgment.** Do not hard-code a decision that needs context ("refund if under \$50") unless that is truly the policy. Let the script return the facts and the policy threshold; let the model apply them to the case and explain.
* **Scripts need their tools.** A script that calls Stripe still needs the provider on the template and `requires.integrations` on the skill.
* **Test the script on its own.** A script is code: give it a small test with known inputs, separate from the agent's evals. See [Skill evals and session evals](https://docs.runtm.com/build/skill-and-session-evals.md).

> [card] [Skills and runbooks](https://docs.runtm.com/build/how-it-works/skills.md)
>
> Ship the script with the runbook that calls it.

> [card] [Skill evals and session evals](https://docs.runtm.com/build/skill-and-session-evals.md)
>
> Test the skill on its own, then the agent end to end.


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