Skip to content

Commit 07bca6a

Browse files
kapaleshreyasclaude
andcommitted
examples: hello.ts — the smallest useful ComputerAgent program
45 lines (incl. comments + blank lines). ~25 lines of real code. Replaces the need to wade through security-agent.ts (169 lines) or binary-artifact-demo.ts (170 lines) just to learn the shape. Covers everything a first-touch user actually needs: - Inline agent definition (no GitHub repo to clone, no GAP folder structure to learn) - LocalSubstrate (no Docker, no E2B account, no Tart VM) - The Claude Agent SDK engine end-to-end — Write tool included - agent.fetchArtifactText() to read back what the agent produced - usage rollup printed at the end so people see token + cost data from line one Live-validated against the real Anthropic API: $ bun run examples/hello.ts Types protect the code Errors caught at compile time Fewer runtime bugs 984 tokens • $0.0468 • session sess_... The other examples stay — they prove richer use cases (multi-turn marketing, security audit, binary artifacts, session resume). But hello.ts is now THE canonical entry point. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0b803cd commit 07bca6a

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

examples/hello.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* The smallest useful ComputerAgent program.
3+
*
4+
* ANTHROPIC_API_KEY=sk-ant-... bun run examples/hello.ts
5+
*
6+
* Tell the agent to write something to a file, then read it back. Shows the
7+
* full loop in ~20 lines: define an agent inline, run it locally with the
8+
* Claude Agent SDK engine, fetch the file the agent produced. No GitHub
9+
* repo, no Docker, no E2B account — just the API key.
10+
*/
11+
import { ComputerAgent, LocalSubstrate } from "computeragent";
12+
13+
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
14+
if (!ANTHROPIC_API_KEY) throw new Error("Set ANTHROPIC_API_KEY first.");
15+
16+
await using agent = new ComputerAgent({
17+
source: {
18+
type: "inline",
19+
manifest: { name: "haiku-bot", version: "0.1.0" },
20+
files: {
21+
"agent.yaml": [
22+
'spec_version: "0.1.0"',
23+
"name: haiku-bot",
24+
"version: 0.1.0",
25+
"model:",
26+
" preferred: claude-haiku-4-5-20251001",
27+
].join("\n"),
28+
"SOUL.md": "Write the requested content to the requested file with the Write tool. Stay terse.",
29+
},
30+
},
31+
harness: "claude-agent-sdk",
32+
runtime: new LocalSubstrate(),
33+
envs: { ANTHROPIC_API_KEY },
34+
options: { permissionMode: "bypassPermissions" },
35+
});
36+
37+
const result = await agent.chat('Write a 3-line haiku about TypeScript to "haiku.txt".');
38+
const haiku = await agent.fetchArtifactText("haiku.txt");
39+
40+
console.log(`\n${haiku ?? "(no file produced)"}\n`);
41+
console.log(
42+
`${result.usage.inputTokens + result.usage.outputTokens} tokens • ` +
43+
`$${result.usage.costUsd?.toFixed(4) ?? "?"} • ` +
44+
`session ${result.sessionId}`,
45+
);

examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7+
"hello": "bun run hello.ts",
78
"wedge1": "bun run wedge1-server.ts",
89
"marketing-agent": "bun run marketing-agent.ts",
910
"security-agent": "bun run security-agent.ts",

0 commit comments

Comments
 (0)