Skip to main content

Prerequisites

Before you begin, make sure you have:
  • Rust 1.70 or higher installed
  • An Anthropic API key, Google AI API key (Gemini), or OpenAI API key
  • Basic familiarity with async Rust and Tokio

Installation

Add PiCrust to your project’s Cargo.toml:

Environment Setup

Set up your LLM provider credentials:

Your First Agent

Create a new file src/main.rs and add the following code:

Run Your Agent

Execute your agent:
You should see the agent’s response stream to your terminal.

Understanding the Code

Let’s break down what’s happening:

1. LLM Provider

Creates an LLM provider that reads credentials from environment variables. The SDK supports multiple providers (Claude, Gemini) through the LlmProvider trait.

2. Agent Runtime

The runtime manages agent lifecycles. It spawns agents as async tasks and maintains a registry of running agents.

3. Session

Sessions persist conversation history to disk automatically (in ./sessions/{id}/). This enables conversation continuity across restarts.

4. Agent Configuration

AgentConfig defines agent behavior. StandardAgent is the main agent implementation that handles the request-response loop.

5. Spawning

Spawns the agent as an async task and returns a handle for communication.

6. Communication

Critical Pattern: Always subscribe to the output stream before sending input. Otherwise, you’ll miss early output chunks.

7. Output Processing

The agent streams output in chunks. TextDelta contains text tokens, Done signals completion.

Adding Tools

Let’s make the agent more useful by adding file access:
Now the agent can read files, write files, and execute shell commands!

Switching Providers

All three providers are interchangeable — just swap one line:
The rest of the code stays the same regardless of which provider you use.
When using OpenAI, disable prompt caching (it’s an Anthropic-specific feature) and use the OpenAI env vars:

Handling Permissions

By default, tools require user permission. Handle permission requests:
Or skip permissions for trusted scenarios (use with caution):

Viewing Conversation History

All conversations are automatically saved to disk. View them:

Complete Example

Here’s a complete interactive agent:

Next Steps

Now that you have a working agent, explore more features:

Core Concepts

Learn about Runtime, Sessions, and Agent States

Built-in Tools

Explore all available tools

Permission System

Understand the three-tier permission system

Hooks

Intercept and modify agent behavior

Common Issues

”Permission denied” errors

Make sure to handle OutputChunk::PermissionRequest or use:

No output streaming

Remember to subscribe before sending input:

API key not found

Set the env vars for your chosen provider:

What’s Next?

You’ve built your first agent! Continue learning:
Ready to dive deeper? Explore the Core Concepts next!