| 1 |
#!/usr/bin/env -S uv run python |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
from anthropic import Anthropic |
| 6 |
|
| 7 |
|
| 8 |
def main() -> None: |
| 9 |
anthropic = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) |
| 10 |
|
| 11 |
# Create an environment |
| 12 |
environment = anthropic.beta.environments.create( |
| 13 |
name="simple-example-environment", |
| 14 |
) |
| 15 |
print("Created environment:", environment.id) |
| 16 |
|
| 17 |
# Create an agent |
| 18 |
agent = anthropic.beta.agents.create( |
| 19 |
name="simple-example-agent", |
| 20 |
model="claude-sonnet-5", |
| 21 |
) |
| 22 |
print("Created agent:", agent.id) |
| 23 |
|
| 24 |
# Create a session |
| 25 |
session = anthropic.beta.sessions.create( |
| 26 |
environment_id=environment.id, |
| 27 |
agent={"type": "agent", "id": agent.id, "version": agent.version}, |
| 28 |
) |
| 29 |
print("Created session:", session.id) |
| 30 |
|
| 31 |
# Send a prompt and stream events until the session goes idle |
| 32 |
print("Streaming events:") |
| 33 |
anthropic.beta.sessions.events.send( |
| 34 |
session.id, |
| 35 |
events=[ |
| 36 |
{ |
| 37 |
"type": "user.message", |
| 38 |
"content": [{"type": "text", "text": "Hello Claude!"}], |
| 39 |
} |
| 40 |
], |
| 41 |
) |
| 42 |
|
| 43 |
with anthropic.beta.sessions.events.stream(session.id) as stream: |
| 44 |
for event in stream: |
| 45 |
print(event.to_json(indent=2)) |
| 46 |
if event.type == "session.status_idle": |
| 47 |
break |
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__": |
| 51 |
main() |