---
id: SI0126
title: "Chain clock tool for models"
url: https://carbium.sh/library/si/chain-clock-tool-for-models
collection: AI and blockchain
category: Model tool calling
revision: 1
updated: 2026-11-23
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Chain clock tool for models

A model asked for the current slot, block height, epoch or transaction count needs all four from one moment. This tool wraps a single getEpochInfo call, names each number in plain terms and keeps the commitment level explicit.

## Four numbers, one method

Slot, block height, epoch and transaction count are four separate counters, and a model that mixes them up answers with confidence and gets it wrong. The slot is the clock: a new one starts about every 200 ms, and a slot whose leader produced nothing gets no block. Block height counts only the slots that did produce a block, so it trails the slot number, and the gap widens with every skipped slot. The epoch is a numbered span of slots, and `slotIndex` says how far into the current one the chain is, out of `slotsInEpoch`. Transaction count is a running total of the transactions the ledger has processed since genesis.

`getEpochInfo` returns the slot (as `absoluteSlot`), `blockHeight`, `epoch`, `slotIndex`, `slotsInEpoch` and `transactionCount` in one response, so all four numbers come from the same moment. Separate `getSlot` and `getBlockHeight` calls can land on different slots, and a model would then subtract two values that were never true together. When the model needs only the slot, `getSlot` is a cheap cached read at 1 cell.

Everything here is read-only. The tool takes no address, no signature and no keypair, which makes it a safe first tool to hand a model.

## Inside an on-chain program

Code running on chain cannot make an RPC call, so a program gets these numbers another way, and only some of them. A program reads the `Clock` sysvar with `Clock::get()` and receives the slot, the epoch and a Unix timestamp. The `EpochSchedule` sysvar holds the slots per epoch. Block height is not in either sysvar, and no sysvar carries a transaction count.

A program that needs one of those two numbers has to take it as instruction data from a client, which means the program cannot trust it. The usual pattern is to read the number off chain with the tool below, show it to the user or the model, and keep every on-chain decision on slot and epoch from the sysvars.

## The tool file

One file on `@solana/kit` 8.6.0, run directly by Node 24. Set `CARBIUM_KEY` first; the file exits with a message when it is missing. `CARBIUM_RPC_URL` overrides the endpoint, which defaults to `https://services.carbium.sh`, and the key goes out in the `x-api-key` header.

`main.ts` (run: `node main.ts`)

```ts
// SI0126 · Chain clock tool for models
// https://carbium.sh/library/si/chain-clock-tool-for-models
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

// Chain clock tool for a model: slot, block height, epoch and transaction count in one read.
// Run: CARBIUM_KEY=... node main.ts
import { createDefaultRpcTransport, createSolanaRpcFromTransport, type Commitment } from "@solana/kit";

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium API key.");
  process.exit(1);
}
const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";
const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));

const LEVELS: readonly string[] = ["processed", "confirmed", "finalized"];
const SLOT_SECONDS = 0.2;

export const tool = {
  name: "chain_clock",
  description:
    "Current Solana slot, block height, epoch, position in the epoch and total transaction count since genesis, read together. A slot lasts about 0.2 s. Block height counts produced blocks and is lower than the slot. epochSecondsLeft is an estimate from that slot time. Read only.",
  parameters: {
    type: "object",
    properties: { commitment: { type: "string", enum: LEVELS, description: "Defaults to confirmed" } },
    additionalProperties: false,
  },
};

export async function chainClock(args: Record<string, unknown> = {}): Promise<string> {
  const commitment = args.commitment ?? "confirmed";
  if (typeof commitment !== "string" || !LEVELS.includes(commitment)) {
    throw new Error("commitment must be processed, confirmed or finalized");
  }
  try {
    const info = await rpc.getEpochInfo({ commitment: commitment as Commitment }).send();
    const slotsLeft = Number(info.slotsInEpoch) - Number(info.slotIndex);
    return JSON.stringify({
      commitment,
      slot: Number(info.absoluteSlot),
      blockHeight: Number(info.blockHeight),
      epoch: Number(info.epoch),
      slotIndex: Number(info.slotIndex),
      slotsInEpoch: Number(info.slotsInEpoch),
      epochSecondsLeft: Math.round(slotsLeft * SLOT_SECONDS),
      transactionCount: info.transactionCount == null ? null : Number(info.transactionCount),
    });
  } catch (e) {
    return JSON.stringify({ error: (e as Error).message, retry: false });
  }
}

if (import.meta.main) {
  const results = await Promise.allSettled(LEVELS.map((commitment) => chainClock({ commitment })));
  results.forEach((r, i) => console.log(r.status === "fulfilled" ? r.value : `${LEVELS[i]} failed: ${r.reason}`));
  try {
    await chainClock({ commitment: "latest" });
  } catch (e) {
    console.log("refused locally:", (e as Error).message);
  }
}
```

`chainClock` is the function your tool loop calls. It checks the commitment argument against three names, makes one `getEpochInfo` call and returns one compact line of JSON. The main block runs the three levels side by side and then asks for a level that does not exist, so you see both the answers and the local refusal. The three levels run through `Promise.allSettled`, so one failed call prints its reason and the others still print.

The kit RPC returns slots, heights and counts as `bigint`, and `JSON.stringify` throws on a `bigint`. Each number goes through `Number` first. Slots, heights and transaction counts stay far below 2^53, so nothing is lost.

## What the model gets back

| Field | Meaning | Note for the tool description |
|---|---|---|
| `slot` | Current slot at the chosen commitment | About 0.2 s per slot |
| `blockHeight` | Blocks produced so far | Lower than the slot, never the same thing |
| `epoch` | Current epoch number | Counts spans of slots |
| `slotIndex`, `slotsInEpoch` | Position in the epoch and its length | Both in slots |
| `epochSecondsLeft` | Slots left times 0.2, rounded | An estimate, say so |
| `transactionCount` | Transactions processed since genesis | `null` when the node leaves it out |

`epochSecondsLeft` exists because models are bad at the arithmetic and often reach for a slot time they remember from older material. Computing it in the tool puts the current slot time in one place. The tool description tells the model the figure is approximate, since a slot is about 200 ms and not exactly that.

## Which commitment the numbers come from

The tool asks for `confirmed` unless the model passes another level. That is explicit on purpose. The JSON-RPC server's own default is finalized, while `@solana/kit` 8.6.0 fills in confirmed when a call leaves commitment out, so a call that omits it gets different answers depending on the client library. Level definitions are in [Solana commitment levels](https://carbium.sh/library/howsolana/solana-commitment-levels).

Since the Alpenglow switch on 2026-10-21, finalized sits 0 to 1 slots behind processed, where it used to trail by about 32. Do not tell a model that finalized is far behind. Because the three reads are separate calls, two of them can come back equal, or briefly out of order, and the tool does not try to tidy that up. A model asked whether a number is final should be given the finalized figure and told that it is one read of a moving chain.

## Where it ends

- The numbers move every 200 ms or so. An answer is true at the moment of the read, so a model that quotes it ten turns later is quoting history. Put the slot in the answer, or call the tool again.
- `epochSecondsLeft` assumes a steady slot time. Skipped slots and changes in the network can move the real end of the epoch.
- `transactionCount` counts what the node reports since genesis. It says nothing about one program, wallet or window of time. For a rate you need two reads and the time between them, and a count per program needs a different tool.
- Block time (a wall-clock timestamp for a slot) is a separate method and is not in this tool.
- Nothing here runs on chain. A program gets its clock from the sysvars described above.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| `JSON.stringify` throws on the response with a BigInt error. | Convert each value with `Number` before building the JSON, as the file does. Slots, heights and counts stay below 2^53. |
| The model treats block height as the slot number, or the other way round. | Say in the tool description that block height counts produced blocks only and sits below the slot, and keep both names in the answer. |
| The model gets two numbers from two calls and subtracts them. | Read everything from one `getEpochInfo` response. Numbers from separate calls can belong to different slots. |
| `transactionCount` is missing from the response and the tool prints 0 or crashes. | Pass it on as `null` and tell the model that a null means the node did not report it. Do not substitute zero. |
| The model estimates the time left in the epoch from an old slot time and is off by a factor of two. | Compute `epochSecondsLeft` in the tool at about 0.2 s per slot and label it approximate in the description. |
| Processed, confirmed and finalized come back equal or in an odd order within one run. | Treat that as normal. The levels sit 0 to 1 slots apart, and separate reads land at slightly different moments. |

## Questions

### Can a Solana program read the transaction count or block height?

No. A program reads the slot and epoch from the `Clock` sysvar and the slots per epoch from `EpochSchedule`. Block height and transaction count are not in a sysvar, so a client has to read them off chain.

### Why one getEpochInfo call and not four?

One response carries the slot, block height, epoch and transaction count from the same moment. Four calls give four readings that can differ by a few slots.

### Which commitment does a call use when I leave it out?

The server defaults to finalized, and `@solana/kit` 8.6.0 sends confirmed. The tool always sends one of the three names so the answer does not depend on the client.

### Is there a lighter call when the model needs only the slot?

Yes. `getSlot` returns the slot alone and is a cheap cached read at 1 cell. Cap the tool calls per task in your loop, because the file does not count them.

## Related

- [Plain-English network status](https://carbium.sh/library/si/plain-english-network-status) (AI and blockchain, SI0033)
- [Solana commitment levels](https://carbium.sh/library/howsolana/solana-commitment-levels) (How to use Solana, HS0003)
- [carbium.sh/docs/rpc](https://carbium.sh/docs/rpc)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
