2025-11-11 14:45:40 +01:00
# LLMX SDK
2025-10-01 13:12:59 -07:00
2025-11-11 14:45:40 +01:00
Embed the LLMX agent in your workflows and apps.
2025-10-05 14:43:34 -07:00
2025-11-11 14:45:40 +01:00
The TypeScript SDK wraps the bundled `llmx` binary. It spawns the CLI and exchanges JSONL events over stdin/stdout.
2025-10-01 13:12:59 -07:00
## Installation
```bash
2025-11-11 14:45:40 +01:00
npm install @llmx/llmx -sdk
2025-10-01 13:12:59 -07:00
```
2025-10-05 14:43:34 -07:00
Requires Node.js 18+.
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
## Quickstart
2025-10-01 13:12:59 -07:00
```typescript
2025-11-11 14:45:40 +01:00
import { LLMX } from "@llmx/llmx -sdk";
2025-10-01 13:12:59 -07:00
2025-11-11 14:45:40 +01:00
const llmx = new LLMX();
const thread = llmx.startThread();
2025-10-05 14:43:34 -07:00
const turn = await thread.run("Diagnose the test failure and propose a fix");
2025-10-01 13:12:59 -07:00
2025-10-05 14:43:34 -07:00
console.log(turn.finalResponse);
console.log(turn.items);
2025-10-01 13:12:59 -07:00
```
2025-10-05 14:43:34 -07:00
Call `run()` repeatedly on the same `Thread` instance to continue that conversation.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-05 14:43:34 -07:00
const nextTurn = await thread.run("Implement the fix");
2025-10-01 13:12:59 -07:00
```
2025-10-05 14:43:34 -07:00
### Streaming responses
2025-10-01 13:12:59 -07:00
2025-11-09 17:37:16 +01:00
`run()` buffers events until the turn finishes. To react to intermediate progress—tool calls, streaming responses, and file change notifications—use `runStreamed()` instead, which returns an async generator of structured events.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-04 19:55:33 -07:00
const { events } = await thread.runStreamed("Diagnose the test failure and propose a fix");
for await (const event of events) {
2025-10-05 14:43:34 -07:00
switch (event.type) {
case "item.completed":
console.log("item", event.item);
break;
case "turn.completed":
console.log("usage", event.usage);
break;
}
2025-10-04 19:55:33 -07:00
}
2025-10-01 13:12:59 -07:00
```
2025-10-05 18:17:50 -07:00
### Structured output
2025-11-11 14:45:40 +01:00
The LLMX agent can produce a JSON response that conforms to a specified schema. The schema can be provided for each turn as a plain JSON object.
2025-10-05 18:17:50 -07:00
```typescript
const schema = {
type: "object",
properties: {
summary: { type: "string" },
status: { type: "string", enum: ["ok", "action_required"] },
},
required: ["summary", "status"],
additionalProperties: false,
} as const;
const turn = await thread.run("Summarize repository status", { outputSchema: schema });
console.log(turn.finalResponse);
```
2025-10-05 18:21:29 -07:00
You can also create a JSON schema from a [Zod schema ](https://github.com/colinhacks/zod ) using the [`zod-to-json-schema` ](https://www.npmjs.com/package/zod-to-json-schema ) package and setting the `target` to `"openAi"` .
2025-10-05 18:17:50 -07:00
```typescript
const schema = z.object({
summary: z.string(),
status: z.enum(["ok", "action_required"]),
});
const turn = await thread.run("Summarize repository status", {
outputSchema: zodToJsonSchema(schema, { target: "openAi" }),
});
console.log(turn.finalResponse);
```
2025-10-20 18:54:59 +02:00
### Attaching images
2025-11-11 14:45:40 +01:00
Provide structured input entries when you need to include images alongside text. Text entries are concatenated into the final prompt while image entries are passed to the LLMX CLI via `--image` .
2025-10-20 18:54:59 +02:00
```typescript
const turn = await thread.run([
{ type: "text", text: "Describe these screenshots" },
{ type: "local_image", path: "./ui.png" },
{ type: "local_image", path: "./diagram.jpg" },
]);
```
2025-10-05 14:43:34 -07:00
### Resuming an existing thread
2025-10-01 13:12:59 -07:00
2025-11-11 14:45:40 +01:00
Threads are persisted in `~/.llmx/sessions` . If you lose the in-memory `Thread` object, reconstruct it with `resumeThread()` and keep going.
2025-10-01 13:12:59 -07:00
```typescript
2025-10-05 14:43:34 -07:00
const savedThreadId = process.env.CODEX_THREAD_ID!;
2025-11-11 14:45:40 +01:00
const thread = llmx.resumeThread(savedThreadId);
2025-10-05 14:43:34 -07:00
await thread.run("Implement the fix");
2025-10-01 13:12:59 -07:00
```
2025-10-01 17:31:13 -07:00
2025-10-05 14:43:34 -07:00
### Working directory controls
2025-10-01 17:31:13 -07:00
2025-11-11 14:45:40 +01:00
LLMX runs in the current working directory by default. To avoid unrecoverable errors, LLMX requires the working directory to be a Git repository. You can skip the Git repository check by passing the `skipGitRepoCheck` option when creating a thread.
2025-10-01 17:31:13 -07:00
```typescript
2025-11-11 14:45:40 +01:00
const thread = llmx.startThread({
2025-10-05 14:43:34 -07:00
workingDirectory: "/path/to/project",
2025-10-01 17:31:13 -07:00
skipGitRepoCheck: true,
});
```