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.You’ve used this in: Trade settlement Step 2
A cross-program invocation (CPI) is one program
calling an instruction on another while it runs.
Any program can call any other program’s instructions.
CPIs without PDA Signers
invoke and Anchor’s CpiContext, with SOL transfer examples.CPIs with PDA Signers
invoke_signed with signer seeds, in Anchor and native Rust.CPI Execution and Privileges
The 11-step execution flow, privilege rules, and reentrancy.
CPI Cost Model and Data Sync
CPI costs, account data synchronization, and return data.
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: Verify the program you invokeThe program you CPI into is an account the caller passes in. If your
program invokes whatever account sits in the In Anchor,
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:Native Rust
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: Forward only the privileges the callee needsSigner 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.
Limits
| Limit | Value | Source |
|---|---|---|
| Max instruction stack depth | 5 (9 with SIMD-0268) | MAX_INSTRUCTION_STACK_DEPTH, MAX_INSTRUCTION_STACK_DEPTH_SIMD_0268 |
| CPI invocation cost | 946 CUs (1,000 before SIMD-0339) | DEFAULT_INVOCATION_COST, INVOKE_UNITS_COST_SIMD_0339 |
| Max PDA signers per CPI | 16 | MAX_SIGNERS |
| Max CPI instruction data | 10 KiB (10,240 bytes) | MAX_INSTRUCTION_DATA_LEN |
| Max return data | 1,024 bytes | MAX_RETURN_DATA |
| Max CPI account infos | 255 (128 before SIMD-0339) | MAX_CPI_ACCOUNT_INFOS, MAX_CPI_ACCOUNT_INFOS_SIMD_0339 |
| CPI serialization cost | 1 CU per 250 bytes | cpi_bytes_per_unit |
| Max account data realloc per CPI | 10,240 bytes (10 KiB) | MAX_PERMITTED_DATA_INCREASE |
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)
and runtime path
(cpi_common).
When seeds are provided, the runtime derives the PDA addresses and adds them to
the valid signers before checking privileges.