The Files API allows you to upload documents once and reference them across multiple requests using a file_id. This eliminates the need to re-encode and re-upload large files for every API call — critical for applications that repeatedly analyze the same documents.
| Method | Example | Best For |
|---|---|---|
| URL | { type: "url", url: "https://..." } | Public documents, quick prototyping |
| Base64 | { type: "base64", data: "..." } | Private files, single-use uploads |
| File ID | { type: "file", file_id: "file_abc123" } | Repeated analysis, multi-turn workflows |
Before sending a request, you can use the Token Counting endpoint to predict exactly how many tokens your message will consume. This is essential for:
// Count tokens before sending
const count = await anthropic.messages.count_tokens({
model: "claude-sonnet-4-6",
messages: [{ role: "user", content: "Your prompt here..." }],
system: "Your system prompt..."
});
console.log(count.input_tokens); // e.g., 1847