Skip to main content
Pi’s AI library supports seamless handoffs between different LLM providers within the same conversation. You can switch models mid-conversation while preserving context, including thinking blocks, tool calls, and tool results.

How It Works

When messages from one provider are sent to a different provider, the library automatically transforms them for compatibility:
  • User and tool result messages are passed through unchanged
  • Assistant messages from the same provider/API are preserved as-is
  • Assistant messages from different providers have their thinking blocks converted to text with <thinking> tags
  • Tool calls and regular text are preserved unchanged
This enables you to start with one model, then switch to another while maintaining conversation continuity.

Quick Example

With Pi SDK

Use cross-provider handoffs in Pi sessions:

Use Cases

Start with a fast model for initial responses, then switch to a more capable model for complex reasoning:

Context Serialization

The Context object can be serialized for persistence or transfer:
If the context contains images (encoded as base64), those will also be serialized.

Provider Compatibility

All providers can handle messages from other providers:

Thinking Block Conversion

When switching providers, thinking blocks are transformed:
Thinking blocks are preserved as-is:

Aborted Messages

Aborted messages can be added to the conversation context and continued:

Best Practices

Example: Multi-Stage Workflow

Here’s a complete example showing a multi-stage workflow with different models:

Next Steps