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
Quick Example
With Pi SDK
Use cross-provider handoffs in Pi sessions:Use Cases
- Fast to Capable
- Specialized Models
- Failover
- Cost Optimization
Start with a fast model for initial responses, then switch to a more capable model for complex reasoning:
Context Serialization
TheContext 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:- Same Provider
- Different Providers
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
- See Programmatic Usage for SDK basics
- See Building Extensions for custom tools
- See Custom Providers for adding new providers