---
title: "Template: Deterministic Code Tools (Billing Math)"
description: Deploy an agent that pairs LLM reasoning with a deterministic JavaScript tool for exact invoice math and threshold checks.
canonical: https://docs.caveman.so/templates/ts-tools
product: Caveman Cloud
tab: Documentation
group: Templates & Quick Setups
---

# Template: Deterministic Code Tools (Billing Math)

> Deploy an agent that pairs LLM reasoning with a deterministic JavaScript tool for exact invoice math and threshold checks.

The `ts-tools` template demonstrates how to bundle custom deterministic tools beside `AGENT.md` under `tools/*.mjs`. Instead of letting a language model estimate proration arithmetic in prose, the agent calls `prorate_refund`, which returns exact integer cents and a boolean approval flag.

## Deploy in 30 seconds

```bash
cvm agent init ts-tools ./ts-tools
cvm agent build ./ts-tools
cvm deploy ./ts-tools --budget 1
cvm run ts-tools "Calculate the exact prorated refund for a $300 monthly invoice used for 10 of 30 days."
```

## Folder layout

```text
ts-tools/
├── AGENT.md                  # Instructions requiring deterministic tool use
└── tools/
    └── prorate_refund.mjs    # Annotated deterministic code tool
```

### `AGENT.md`

```markdown
---
name: ts-tools
description: Agent combining deterministic QuickJS code tools with durable approvals.
computer: code
model: claude-sonnet-5-5
---

# Billing Math Agent

Use the deterministic `prorate_refund` code tool for all invoice math. Never estimate proration in prose.
```

### `tools/prorate_refund.mjs`

```js
// @caveman-tool {"description":"Compute exact prorated refund amount in cents for unused days in a billing cycle.","effect":"read","input_schema":{"type":"object","required":["total_cents","days_used","cycle_days"],"properties":{"total_cents":{"type":"integer","minimum":0},"days_used":{"type":"integer","minimum":0},"cycle_days":{"type":"integer","minimum":1}}}}
export default function execute({ total_cents, days_used, cycle_days }) {
  const remaining = Math.max(0, cycle_days - days_used);
  const refund_cents = Math.round((total_cents * remaining) / cycle_days);
  return { refund_cents, remaining_days: remaining, requires_approval: refund_cents > 50000 };
}
```

## How tool annotations work

The leading `// @caveman-tool {...}` header declares:

- `description`: shown in the agent's tool schema.
- `effect`: declares side-effect class (`read`, `write`, `idempotent`, or `external`) so durable step fencing handles retries safely without repeating external actions.
- `input_schema`: standard JSON Schema validated before `execute(...)` runs.

## Test locally with hot reload

```bash
cvm agent dev ./ts-tools
```

Edit `tools/prorate_refund.mjs` or `AGENT.md` and `cvm agent dev` recompiles `agent.json` automatically so you can test edge cases (`days_used > cycle_days`, refunds over `$500`) in seconds.

## Verify and undo

```bash
cvm agent build ./ts-tools --out ./dist-build
cvm deployments archive ts-tools --yes
```

## Limits

- `computer: code` availability depends on the target Cloud installation's enabled sandbox tiers. When a tier is disabled on a target install, `cvm deploy` still stores the build and prints what a session would be refused for. Use `computer: none` with custom tools or `computer: linux` when a full Linux sandbox is needed.
