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

# Terminal Resources

> Terminal resources, the flow epilogue and sweepTo, and how estimates and floors are reported on the response.

Any balance the execution proxy still holds when the flow ends stays there unless the flow moves it. Ops that deliver to a recipient move their own output. The **flow epilogue**, or its shorthand `run.sweepTo`, moves the rest.

## Terminal resources

A resource is **terminal** when no op in the flow consumes it; see [Resource Model](https://docs.li.fi/composer/composer-api/concepts/resources-and-ports.md#terminal-resources-and-their-owner). Every terminal appears in the response's `outputs` with `role: "terminal"`, an `owner` and a `delivery`. An unspent resource input is reported under the key `input.<name>`.

| Terminal | Example | Owner | Moved by the epilogue? |
| - | - | - | - |
| Delivered to a recipient | `core.transfer.transferred`, `lifi.zap.amountOut` with `recipient` bound | The recipient | No: it already left the proxy. |
| Arrives later | `lifi.bridge.amountOut`, `lifiIntents.order.amountOut`, `continuation.settle.output` | The destination | No: it is never on the proxy. |
| Still on the proxy, available now | A vault receipt, an unbound split output, an unspent input | The epilogue's destination, or the proxy if there is none | Yes. |

## The flow epilogue

The `epilogue` is an optional top-level Flow field. Its transfers run after the last node:

```json theme={"system"}
{
  "epilogue": {
    "forwards": [
      { "resource": { "kind": "erc20", "token": "0x4200000000000000000000000000000000000006", "chainId": 8453 }, "to": "0x2222222222222222222222222222222222222222" }
    ],
    "remainder": { "$ref": "context.sender" }
  }
}
```

* **Named forwards.** Each entry in `forwards` moves the proxy's **whole balance** of one resource to its `to` address, and the response reports the amount the simulation measured. Name each resource at most once; it must be on the flow's chain, and the native coin is `{ "kind": "native", "chainId": … }`.
* **Remainder.** `remainder` moves the whole proxy balance of every other token that is a proxy-held terminal of this flow. It does not touch tokens the flow never produced as a terminal, and a remainder with nothing to move does nothing.
* **Targets.** `to` and `remainder` take a literal address or `{ "$ref": "context.sender" }`. No other ref is accepted.

An epilogue needs `forwards`, `remainder`, or both. It never moves terminals delivered to a recipient or terminals that arrive later.

### Set the epilogue from the SDK

The flow builder has no epilogue method. Build the request with `sdk.request`, add `epilogue` to the flow document, and send it with `sdk.client.compile`:

```ts theme={"system"}
import { createComposeSdk, materialisers, resources } from '@lifi/composer-sdk';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

const USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; // USDC on Base
const WETH = '0x4200000000000000000000000000000000000006'; // WETH on Base

const builder = sdk.flow(8453, {
  inputs: { amountIn: resources.erc20(USDC, 8453) },
});

builder.lifi.swap('swap', {
  bind: { amountIn: builder.inputs.amountIn },
  config: { resourceOut: resources.erc20(WETH, 8453), slippage: 0.005 },
});

const request = sdk.request(builder.build(), {
  inputs: { amountIn: materialisers.directDeposit({ amount: '1000000' }) },
  signer: '0x1111111111111111111111111111111111111111',
});

const result = await sdk.client.compile({
  ...request,
  flow: {
    ...request.flow,
    epilogue: {
      forwards: [
        {
          resource: resources.erc20(WETH, 8453),
          to: '0x2222222222222222222222222222222222222222',
        },
      ],
      remainder: { $ref: 'context.sender' },
    },
  },
});

if (result.status === 'success') {
  for (const forward of result.epilogue?.forwards ?? []) {
    console.log(forward.to, forward.amount);
  }
}
```

The swap's WETH goes to `0x2222…2222`. Any USDC the swap left unspent (its `unspentIn` output) goes back to the signer through the remainder.

## `sweepTo`

`run.sweepTo` on the compile request is shorthand for `epilogue.remainder`. It takes the same targets:

* **`builder.context.sender`.** Sweep to the transaction signer, as most flows do. On the wire it is `{ "$ref": "context.sender" }`.
* **A literal address.** Sweep to that address.
* **Omitted.** No sweep. Terminal resources stay on the per-signer execution proxy, for example to accumulate a balance across several submissions.

A request with both `run.sweepTo` and `flow.epilogue` is rejected with a `validation_error` at `run.sweepTo`. To deliver one token to a third party, use an op's `recipient` or a named epilogue forward instead of sweeping everything there.

The sweep moves every proxy-held terminal that is available now. It does not move resources another op consumes, terminals delivered to a recipient, terminals that arrive later, or handles. An op that does not spend all of its input reports the rest as its own output, for example `lifi.swap`'s `unspentIn`, and the sweep moves it if nothing binds it. A `core.split` relies on this to keep part of an input: with `config: { bps: 8000 }`, bind only its `a` handle to a swap, and the sweep returns the unbound 20% (`b`) to the sender in the same transaction.

### Non-transferable terminal resources

> [!WARNING]
>
> The remainder moves the **whole balance** of each terminal token on the proxy, and some balances cannot be transferred. The common case is **aTokens or other collateral receipts that back an open debt**: Aave reverts a transfer that would leave the position under-collateralised, so a remainder or `sweepTo` over such a balance makes the **whole transaction revert**.

When a flow deliberately leaves a position on the proxy, for example a borrow or leverage flow that keeps collateral in place, **do not** set a remainder or `sweepTo`. Move only the loose tokens: name them in `epilogue.forwards`, or read each with `core.balanceOf` (the owner defaults to the proxy) and move it with `core.transfer`:

```ts theme={"system"}
import { createComposeSdk, resources } from '@lifi/composer-sdk';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

const USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; // USDC on Base
const WETH = '0x4200000000000000000000000000000000000006'; // WETH on Base

const builder = sdk.flow(8453, {
  inputs: { collateral: resources.erc20(WETH, 8453) },
});

// ...ops that leave a position on the proxy and some loose USDC...

// Read only the loose USDC on the proxy and transfer it to the signer.
const loose = builder.core.balanceOf('read-usdc', {
  bind: {},
  config: { token: USDC },
});

builder.core.transfer('payout', {
  bind: { amount: loose.balance, recipient: builder.context.sender },
  config: {},
});
```

`core.transfer` moves the whole amount it is bound to, and the recipient owns its `transferred` output.

## Supplying input amounts

A resource input's value in `run.inputs` is either a materialiser or a plain integer. A materialiser funds or measures the amount when the transaction runs: `directDeposit` pulls it from the signer, while `balanceOf` and `call` measure an amount that is already there or that a call produces. A plain integer says the proxy **already holds** that amount; nothing is pulled from the signer. See [Wire runtime values](https://docs.li.fi/composer/composer-api/guides/build-a-flow.md#wire-runtime-values) and the [materialisers catalog](https://docs.li.fi/composer/composer-api/materialisers.md).

Amounts that depend on an earlier step, such as the shares a vault deposit mints, are read onchain during the transaction. The only amounts fixed at compile time are provider quotes.

## Amounts on the response

Every token output port has an entry in `outputs`, keyed `<nodeId>.<port>`, plus `input.<name>` for unspent inputs. The SDK decodes amounts to `bigint`. [The compile response](https://docs.li.fi/composer/composer-api/concepts/execution-model.md#the-compile-response) lists every response field; each `outputs` entry carries:

* **`role`**: `consumed` (a later node, named in `consumedBy`, spends it) or `terminal`.
* **`owner`** (terminal only): who holds the output after the flow. With `sweepTo` set to the signer, proxy-held terminals report the signer.
* **`delivery`** (terminal only): `{ when: "now" }`, or `{ when: "future", etaSeconds?, provider?, trackingId? }` for an output that arrives later. See [Future resources](https://docs.li.fi/composer/composer-api/concepts/resources-and-ports.md#future-resources).
* **`amount.estimate`**: the expected amount. `basis: "simulated"` when the simulation measured it, `basis: "quoted"` when it comes from a provider quote. Display it as "you will receive ≈ X".
* **`amount.minimum`**: the guaranteed floor, enforced onchain. `basis` says where it comes from: `guard` (a guard floor), `op` (the op's own minimum, such as a swap's `minOut`), `input` (an unspent input's bound amount), or `declared` (a `providesMinimum` port's estimate, used when no other floor exists). Display it as "you will receive **at least** X".

**An absent `minimum` means no floor exists.** There is no zero placeholder: an unguarded split output reports only an estimate. When a guard floor and an op floor both exist, the larger wins; see [Guaranteed minimums](https://docs.li.fi/composer/composer-api/concepts/simulation-and-guards.md#guaranteed-minimums). On a 206 (allow-revert) response nothing was measured, so estimates are quoted and floors come only from ops and inputs.

For a `continuation.settle` output, `minimum` is the minimum received, which the settlement contract checks onchain, and `estimate` is an expected outcome that is never enforced; see [Cross-chain flows](https://docs.li.fi/composer/composer-api/concepts/cross-chain-flows.md).

The `epilogue` block on a 200 response reports each named forward with its `resource`, `to` and measured `amount`, and the resolved `remainder` address. The remainder itself is not measured; it shows up in `outputs` only as the new `owner`.

## See also

* [Simulation and guards](https://docs.li.fi/composer/composer-api/concepts/simulation-and-guards.md): where guard floors come from.
* [Execution Model](https://docs.li.fi/composer/composer-api/concepts/execution-model.md#the-compile-response): every field of the compile response.
* [Cross-chain flows](https://docs.li.fi/composer/composer-api/concepts/cross-chain-flows.md): outputs that land later, from an escrow.


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