| 7 | |
| 8 | |
| 9 | def main() -> None: |
| 10 | anthropic = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) |
| 11 | |
| 12 | # Create an environment |
| 13 | environment = anthropic.beta.environments.create( |
| 14 | name="files-example-environment", |
| 15 | ) |
| 16 | print("Created environment:", environment.id) |
| 17 | |
| 18 | # Create an agent with the built-in toolset and an always-allow permission policy |
| 19 | agent = anthropic.beta.agents.create( |
| 20 | name="files-example-agent", |
| 21 | model="claude-sonnet-5", |
| 22 | tools=[ |
| 23 | { |
| 24 | "type": "agent_toolset_20260401", |
| 25 | "default_config": { |
| 26 | "enabled": True, |
| 27 | "permission_policy": {"type": "always_allow"}, |
| 28 | }, |
| 29 | } |
| 30 | ], |
| 31 | ) |
| 32 | print("Created agent:", agent.id) |
| 33 | |
| 34 | # Upload a file |
| 35 | file = anthropic.beta.files.upload( |
| 36 | file=Path(__file__).parent / "data.csv", |
| 37 | ) |
| 38 | print("Uploaded file:", file.id) |
| 39 | |
| 40 | # Create a session with the file mounted as a resource |
| 41 | session = anthropic.beta.sessions.create( |
| 42 | environment_id=environment.id, |
| 43 | agent={"type": "agent", "id": agent.id, "version": agent.version}, |
| 44 | resources=[ |
| 45 | { |
| 46 | "type": "file", |
| 47 | "file_id": file.id, |
| 48 | "mount_path": "data.csv", |
| 49 | } |
| 50 | ], |
| 51 | ) |
| 52 | print("Created session:", session.id) |
| 53 | |
| 54 | resources = anthropic.beta.sessions.resources.list(session.id) |
| 55 | print("Listed session resources:", resources.data) |
| 56 | |
| 57 | # Send a prompt asking the agent to read the mounted file and stream events |
| 58 | print("Streaming events:") |
| 59 | anthropic.beta.sessions.events.send( |
| 60 | session.id, |
| 61 | events=[ |
| 62 | { |
| 63 | "type": "user.message", |
| 64 | "content": [ |
| 65 | { |
| 66 | "type": "text", |