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

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Instruction Introspection

> How Solana programs inspect other instructions in the same transaction using the Instructions sysvar. Load instructions by absolute index or relative offset. CPI inner instructions are not accessible.

<Info>
  **Summary**

  The Instructions sysvar (`Sysvar1nstructions1111111111111111111111111`) lets a
  program read all top-level instructions in the current transaction by index or
  relative offset. CPI inner instructions are not accessible.
</Info>

## Instructions sysvar

Programs can inspect all top-level instructions in the current transaction via
the **Instructions sysvar** (`Sysvar1nstructions1111111111111111111111111`).
This lets a program inspect the other instructions in the same transaction, for
example to verify that a specific instruction is in the same transaction as the
instruction it's executing.

<Info>
  The Instructions sysvar only contains top-level instructions from the
  transaction message. Inner instructions invoked via CPI are not accessible
  through this sysvar.
</Info>

The instructions sysvar is not accessed through the normal `Sysvar` trait.
Instead, it is accessed through free functions in the
[`solana_instructions_sysvar`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/instructions-sysvar/src/lib.rs#L1)
crate.

The sysvar data is serialized with a custom binary layout:

| Offset       | Size   | Description                                    |
| ------------ | ------ | ---------------------------------------------- |
| 0            | 2      | `num_instructions` (u16, little-endian)        |
| 2            | 2 \* N | Byte offsets for each instruction (u16 each)   |
| varies       | varies | Serialized instruction data                    |
| last 2 bytes | 2      | Current instruction index (u16, little-endian) |

Each serialized instruction contains: the number of accounts (u16), the accounts
as 33-byte entries (1 flag byte + 32-byte pubkey), the program ID (32 bytes),
the data length (u16), and the raw data bytes.

Key functions:

- [`load_current_index_checked(account_info)`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/instructions-sysvar/src/lib.rs#L151):
  Returns the index of the currently executing instruction. The runtime
  [updates](https://github.com/anza-xyz/agave/blob/v3.1.8/transaction-context/src/lib.rs#L356-L359)
  this value each time a new top-level instruction begins.
- [`load_instruction_at_checked(index, account_info)`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/instructions-sysvar/src/lib.rs#L246):
  Deserializes the instruction at the given absolute index.
- [`get_instruction_relative(offset, account_info)`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/instructions-sysvar/src/lib.rs#L267):
  Loads an instruction relative to the current one (e.g., `-1` for the previous
  instruction, `1` for the next).
