Files
A file is either a document you upload for an agent to read during a session, or an output an agent writes during a session. Upload a file, then attach it when you create a session. Each upload is stored in the Context app, in the Drive of the user who owns the API key.
The agent writes the files it produces to /mnt/session/outputs/, as on Anthropic's API. To get them, list files
with scope_id set to the session's ID, then download each one. They stay listed after you archive the session.
Deleting a file is permanent: it can't be restored through this API or the app. You can delete only files you
uploaded. Files a session produced can't be deleted or attached to another session. You can't delete a file
attached to a session that isn't archived. The request returns 400. Archive the session first.
The file object
| Field | Type | Description |
|---|---|---|
id | string | The file's ID. An uploaded file's ID is a UUID. A file a session produced has an ID that starts with sesnfile_. Treat both as opaque. |
type | string | Always file. |
filename | string | The name sent with the upload. For a file a session produced, this is its path in the session's output folder, such as charts/q3.csv. |
mime_type | string | The media type sent with the upload, or application/octet-stream if none was sent. |
size_bytes | integer | The size in bytes, or 0 if the size wasn't recorded. |
created_at | string | When the file was uploaded or written, as an RFC 3339 timestamp. |
downloadable | boolean | Always true. |
scope | object | The session that produced the file, as {type: "session", id}, where id is the session's ID. Uploaded files have no scope field. |
Example
Code
Differences from Anthropic's API
This API follows Anthropic's Managed Agents API, with these differences:
- Upload accepts
expires_in_secondsfrom 3600 to 7776000, but doesn't apply it. Files don't expire, and the file object has noexpires_at. - List files doesn't support
ids. Sending it returns400. - A list response holds at most 100 files per page. Anthropic's API allows up to 1,000.
- An upload can be as large as your deployment's upload limit, which defaults to 5 GiB. Anthropic's limit is 500 MB.
- The files a session produced can't be deleted or attached to another session through this API.
- List files also accepts
after_idandbefore_id, and returnshas_more,first_idandlast_id, which Anthropic's list response doesn't include. - Deleting a file that a session still uses returns
400. See Delete a file.
For every difference in one place, see Coming from Anthropic's API.
List files
Lists the files this key or its owner uploaded, or the files a session produced, newest first.
Without scope_id, the list holds files uploaded with a Task API key: the files this key uploaded, and the
files that this key's owner owns. With scope_id set to a session ID, the list holds the files that session's
agent produced, each with a scope field that names the session. That list leaves out the session's attached
files, the files the platform stores for the session (such as its transcript), and large tool results saved
during the session. The list stays the same after the session is archived.
Send at most one of page, after_id and before_id. Both SDKs paginate with page.
The key must have the files:read capability. For what each capability allows, see Keys.
Response
Returns 200 with a list of file objects, newest first.
| Field | Description |
|---|---|
data | The files on this page. |
first_id | The ID of the first file on the page. |
last_id | The ID of the last file on the page. |
has_more | true when more files remain in the direction you are paging: older files for page and after_id, newer files for before_id. |
next_page | The value to pass as page for the next page. It is null on the last page and on a before_id page. |
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | limit is outside 1 to 100. Send a value from 1 to 100. |
| 400 | invalid_request_error | The request sends more than one of page, after_id and before_id. Send only one of them. |
| 400 | invalid_request_error | The request sends ids, which this API doesn't take. Remove it. |
| 404 | not_found_error | after_id or before_id names a file that isn't on this list. Pass first_id or last_id from a previous page of this list. |
| 404 | not_found_error | scope_id names a session this key can't read. Pass the ID of a session this key can read. |
query Parameters
limitThe maximum number of files to return. Must be between 1 and 100.
The maximum number of files to return. Must be between 1 and 100.
pageThe next_page value from the previous response. Leave it out to get the first page.
The next_page value from the previous response. Leave it out to get the first page.
after_idThe ID of a file. Returns files older than that file. Pass last_id from the previous page.
The ID of a file. Returns files older than that file. Pass last_id from the previous page.
before_idThe ID of a file. Returns files newer than that file. Pass first_id from the previous page.
The ID of a file. Returns files newer than that file. Pass first_id from the previous page.
scope_idA session ID. Returns the files that session's agent produced instead of the default list.
A session ID. Returns the files that session's agent produced instead of the default list.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
List files › Responses
Returns 200 with a list of file objects, newest first.
next_pagehas_morefirst_idlast_idUpload a file
Uploads one file to the Drive of the user who owns the key.
Send multipart/form-data with one file field, whose part includes a filename, and optionally an
expires_in_seconds field. The server accepts any file type. In a body over 10 MiB, expires_in_seconds must
come before file. The Python and .NET SDKs send it first. With the TypeScript SDK, write expires_in_seconds
before file in the object you pass to files.upload.
These limits apply:
- A file can be up to your deployment's upload limit, which is 5 GiB unless your administrator set it lower.
- Send a
Content-Lengthheader. cURL and the SDKs send one for you. A request without one must use chunked encoding and be 10 MiB or smaller. - Each key can have up to 4 uploads in progress on one server instance, and each instance takes up to 8 in
total across all keys. Keep 4 or fewer uploads in progress, and retry on
429.
If the request fails partway, the server keeps nothing.
The key must have the files:write capability. For what each capability allows, see Keys.
Response
Returns 200 with the file object. For every field, see Files.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | The form has a field other than file and expires_in_seconds. Remove the other fields. |
| 400 | invalid_request_error | expires_in_seconds isn't a whole number between 3600 and 7776000. Send a value in that range, or omit it. |
| 400 | invalid_request_error | expires_in_seconds comes after file in a body over 10 MiB. Send expires_in_seconds before file, or omit it. |
| 400 | invalid_request_error | The form has no file field, or more than one, or the filename is empty. Send exactly one file field with a non-empty filename. |
| 400 | invalid_request_error | The body doesn't match its Content-Length: bytes follow the closing boundary, or the body ends early. The server keeps nothing. Send an exact Content-Length. |
| 413 | request_too_large | The file is over your deployment's upload limit (5 GiB unless your administrator set it lower), or a chunked request is over 10 MiB. The message gives the limit. If the file is over the upload limit, send a smaller file. If the request is chunked and over 10 MiB, send it again with a Content-Length header. |
| 429 | rate_limit_error | This key already has 4 uploads in progress on the server instance that answered, or 8 are in progress there for all keys. Wait for the time in Retry-After, then retry. |
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Upload a file › Request Body
fileThe file, with a filename. If the part has no media type, the server stores the file as application/octet-stream.
expires_in_secondsThe number of seconds until the file expires on Anthropic's API. Must be a whole number between 3600 and 7776000. It isn't applied: the file is kept until you delete it, and the file object has no expires_at.
Upload a file › Responses
Returns 200 with the file object. For every field, see Files.
idcreated_atfilenamemime_typesize_bytestypedownloadableGet a file
Returns a file's metadata.
To get the file's content, see Download a file.
The key must have the files:read capability. For what each capability allows, see Keys.
Response
Returns 200 with the file object. For every field, see Files.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 404 | not_found_error | No file with this ID exists, or this key can't read it or the session that produced it. Check the ID from the upload or from the list. |
path Parameters
file_idThe ID of the file to retrieve.
The ID of the file to retrieve.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Get a file › Responses
Returns 200 with the file object. For every field, see Files.
idcreated_atfilenamemime_typesize_bytestypedownloadableDelete a file
Permanently deletes an uploaded file.
The file doesn't go to the Drive trash, and it can't be restored through this API or the app. A later request
for it returns 404. If the server can't delete the stored copy, the request returns 500 and the copy stays in
the owner's Drive. See the errors below.
A service account key can delete only the files it uploaded. This endpoint can't delete files that a session produced, or a file that a session or run still uses. See the errors below.
The key must have the files:write capability. For what each capability allows, see Keys.
Response
Returns 200 with an object that holds the file's id and a type of file_deleted.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | The file is attached to a session that isn't archived, or is used by a run that hasn't finished. Archive the session, or wait for the run to finish, then retry. |
| 400 | invalid_request_error | The file was produced by a session. Download it with Download a file, or remove it from the session's folder in Drive. |
| 403 | permission_error | The key lacks files:write. Use a key that has files:write. |
| 403 | permission_error | The key's owner can't edit the Drive folder that holds the file. Use a key whose owner can edit that folder. |
| 403 | permission_error | A service account key tried to delete a file it didn't upload. Use the personal key of the user who owns the file. |
| 404 | not_found_error | No file with this ID exists, or this key can't read it. Check the ID against List files. If the ID is correct, check that the key can read the file. |
| 500 | api_error | The API no longer returns the file, but its stored copy couldn't be deleted. The message says where the copy remains in the owner's Drive. Delete it there, or quote request_id when you contact support. |
path Parameters
file_idThe ID of the file to delete.
The ID of the file to delete.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Delete a file › Responses
Returns 200 with an object that holds the file's id and a type of file_deleted.
idtypeDownload a file
Returns a file's content exactly as it was uploaded or as a session wrote it.
For an uploaded file, the file's owner must be able to read it in Drive. For a file a session produced, the key
must be able to read that session and the file in Drive. If a check fails, the request returns 404.
The server streams the content, so the response has no content-length header. To read the file's name, size
and media type without downloading it, see Get a file.
The key must have the files:read capability. For what each capability allows, see Keys.
Response
Returns 200 with the file's bytes. The content-type response header is the media type stored with the file, or application/octet-stream if none was sent.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 402 | billing_error | The file is a session's output, and your deployment requires a paid plan or credits before you can download session output. Add a plan or credits in the app, then retry. |
| 404 | not_found_error | No file with this ID exists, or this key can't read the file or the session that produced it. Check the ID against the upload response or List files. If the ID is correct, check the access rules on this page. |
| 404 | not_found_error | The file's content is no longer stored. If you uploaded the file, upload it again. |
path Parameters
file_idThe ID of the file to download.
The ID of the file to download.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Download a file › Responses
Returns 200 with the file's bytes. The content-type response header is the media type stored with the file, or application/octet-stream if none was sent.