A minimal terminal chat example for the 21st Python SDK. It connects to a deployed agent, creates a sandbox and thread, then streams responses directly in your terminal.
The Python SDK uses Python-style snake_case for method arguments, but response objects keep the relay's camelCase fields.
- CLI-only chat with no frontend
- Streaming text output from a deployed agent
- One sandbox + one thread per process so follow-up turns continue the same session
- Optional
.envconfig for quick local testing
- Python 3.9+
- A deployed 21st Agent
- A 21st API key (
an_sk_...)
| Variable | Where | Description |
|---|---|---|
API_KEY_21ST |
.env or shell |
Server-side API key |
AGENT_SLUG |
.env or shell |
Deployed agent slug to chat with |
git clone https://github.com/21st-dev/an-examples.git
cd an-examples/python-terminal-chatpython3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pippython -m pip install 21st-sdkpython -m pip install .
cp .env.example .env
# Edit .env with your API_KEY_21ST and AGENT_SLUGThis installs the small CLI from this folder so you can run python-terminal-chat from your shell.
python-terminal-chatType messages and press Enter.
Commands:
/new— start a fresh thread in the same sandbox/exit— quit the program
- Loads
API_KEY_21STandAGENT_SLUGfrom.envor your shell - Creates
AgentClient(api_key=..., base_url=...) - Creates a sandbox for the deployed agent
- Creates a thread inside that sandbox
- For each user message:
- appends the message to local chat history
- calls
client.threads.run(...) - reads the SSE stream from
result.response - prints
text-deltachunks to the terminal - refreshes the thread from the API and reuses the persisted
messagesarray for the next turn
$ python-terminal-chat
Connected to agent: support-agent
Sandbox: 1d5f...
Runtime sandbox: e2b_abc123...
Thread: 97a1...
you> summarize what this agent can do
assistant> I can answer questions, use tools, and work inside a persistent sandbox.
you> /new
Started a new thread: 2f9d...
you> quit
python-terminal-chat/
├── .env.example
├── pyproject.toml
├── README.md
└── src/
└── python_terminal_chat/
├── __init__.py
└── cli.py