> ## 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.

# Cross-program invocation

> Cross-program invocation (CPI): how Solana programs call other programs with invoke and invoke_signed, and how PDAs sign.

## In short

A cross-program call (CPI) is one program calling another inside the same transaction, the way your program would call the Token program to move tokens.

<div className="sol-usedin not-prose" data-concept="cpi">
<span className="sol-usedin-label">You've used this in:</span> [Trade settlement <span className="sol-usedin-step">Step 2</span>](/developers/docs/defi/settle-trade/browser)
</div>

A cross-program invocation (CPI) is one [program](/developers/docs/core/programs)
calling an [instruction](/developers/docs/core/instructions) on another while it runs.
Any program can call any other program's instructions.

<Frame caption="Program A calls Program B within the same instruction, one level deeper in the call stack.">
  <img className="block dark:hidden" src="/docs-assets/assets/docs/core/cpi/cpi-light.svg" alt="Program A calls Program B within the same instruction, one level deeper in the call stack." />

  <img className="hidden dark:block" src="/docs-assets/assets/docs/core/cpi/cpi.svg" alt="Program A calls Program B within the same instruction, one level deeper in the call stack." />

  <img className="block dark:hidden" src="/docs-assets/assets/docs/core/cpi/cpi-sm-light.svg" alt="Program A calls Program B within the same instruction, one level deeper in the call stack." />

  <img className="hidden dark:block" src="/docs-assets/assets/docs/core/cpi/cpi-sm.svg" alt="Program A calls Program B within the same instruction, one level deeper in the call stack." />
</Frame>

<Columns cols={2}>
  <Card title="CPIs without PDA Signers" href="/developers/docs/core/cpi/cpi-without-pda">
    `invoke` and Anchor's CpiContext, with SOL transfer examples.
  </Card>
  <Card title="CPIs with PDA Signers" href="/developers/docs/core/cpi/cpi-with-pda">
    `invoke_signed` with signer seeds, in Anchor and native Rust.
  </Card>
  <Card
    title="CPI Execution and Privileges"
    href="/developers/docs/core/cpi/cpi-execution"
  >
    The 11-step execution flow, privilege rules, and reentrancy.
  </Card>
  <Card
    title="CPI Cost Model and Data Sync"
    href="/developers/docs/core/cpi/cpi-cost-model"
  >
    CPI costs, account data synchronization, and return data.
  </Card>
</Columns>

## Key facts

- **Privilege extension**: Account privileges (signer, writable) extend from
  caller to callee. A callee cannot escalate privileges
  beyond what the caller passed.
- **Shared compute budget**: A callee's CU consumption reduces the caller's
  remaining budget.
- **Reentrancy**: Direct self-recursion is allowed (A->A->A). Indirect
  reentrancy is not (A->B->A returns `ReentrancyNotAllowed`).

<Security title="Verify the program you invoke">
  The program you CPI into is an account the caller passes in. If your
  program invokes whatever account sits in the `token_program` position, an
  attacker can pass their own program. It receives every account and
  privilege you forward, can return success without doing the work, and can
  use your users' signatures. Check the program ID before invoking:

  ```rust title="Native Rust"
  if token_program.key != &spl_token::ID {
      return Err(ProgramError::IncorrectProgramId);
  }
  ```

  In Anchor, `Program<'info, Token>` checks the ID for one program, and
  `Interface<'info, TokenInterface>` accepts either the Token Program or the
  Token Extensions Program. Instruction builders such as
  `system_instruction::transfer` hard-code the program ID, which is safe as
  long as you don't overwrite it.
</Security>

<Security title="Forward only the privileges the callee needs">
  Signer and writable privileges pass from caller to callee for the duration
  of the CPI. Passing a user's signer account into a program you don't
  control gives that program the user's signature for anything it does
  during the call, including CPIs of its own. Forward signer accounts only
  to programs you trust, and only the accounts that instruction needs.
</Security>

## Limits

| Limit                            | Value                                                                 | Source                                                                                                                                                                                                                                                          |
| -------------------------------- | --------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Max instruction stack depth      | 5 (9 with [SIMD-0268](https://solana.com/upgrades/cpi-nesting-depth)) | [`MAX_INSTRUCTION_STACK_DEPTH`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/execution_budget.rs#L8), [`MAX_INSTRUCTION_STACK_DEPTH_SIMD_0268`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/execution_budget.rs#L10) |
| CPI invocation cost              | 946 CUs (1,000 before SIMD-0339)                                      | [`DEFAULT_INVOCATION_COST`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/execution_budget.rs#L21), [`INVOKE_UNITS_COST_SIMD_0339`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/execution_budget.rs#L23)              |
| Max PDA signers per CPI          | 16                                                                    | [`MAX_SIGNERS`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/cpi.rs#L62)                                                                                                                                                                   |
| Max CPI instruction data         | 10 KiB (10,240 bytes)                                                 | [`MAX_INSTRUCTION_DATA_LEN`](https://github.com/anza-xyz/agave/blob/v3.1.8/transaction-context/src/lib.rs#L33)                                                                                                                                                  |
| Max return data                  | 1,024 bytes                                                           | [`MAX_RETURN_DATA`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/cpi/src/lib.rs#L329)                                                                                                                                                             |
| Max CPI account infos            | 255 (128 before SIMD-0339)                                            | [`MAX_CPI_ACCOUNT_INFOS`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/cpi.rs#L122), [`MAX_CPI_ACCOUNT_INFOS_SIMD_0339`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/cpi.rs#L124)                                    |
| CPI serialization cost           | 1 CU per 250 bytes                                                    | [`cpi_bytes_per_unit`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/execution_budget.rs#L205)                                                                                                                                              |
| Max account data realloc per CPI | 10,240 bytes (10 KiB)                                                 | [`MAX_PERMITTED_DATA_INCREASE`](https://github.com/anza-xyz/solana-sdk/blob/clock%40v2.2.3/account-info/src/lib.rs#L17)                                                                                                                                         |

## `invoke` vs `invoke_signed`

| Function        | Use case                                                                     | PDA signing           |
| --------------- | ---------------------------------------------------------------------------- | --------------------- |
| `invoke`        | CPIs where all required signers have already signed the original transaction | No                    |
| `invoke_signed` | CPIs where the calling program needs to sign on behalf of a PDA it owns      | Yes, via signer seeds |

`invoke` is `invoke_signed` with an empty signer seeds array. Both use the same
syscall
([`sol_invoke_signed_rust`](https://github.com/anza-xyz/agave/blob/v3.1.8/syscalls/src/cpi.rs#L11-L33))
and runtime path
([`cpi_common`](https://github.com/anza-xyz/agave/blob/v3.1.8/program-runtime/src/cpi.rs#L802-L925)).
When seeds are provided, the runtime derives the PDA addresses and adds them to
the valid signers before checking privileges.
