# Twigg SDKs Official clients for TypeScript and Python, and a CLI that keeps your Twigg configuration as code. Source, examples and full READMEs: https://github.com/Twigg-AI-Ltd/sdk You do not need an SDK. The API is plain HTTP, and there is no client-side state to keep in sync; the [quickstart](https://twigg.ai/docs/quickstart.md) uses nothing else. The SDKs take care of the fiddly parts: - **The stream.** Server-sent events are parsed and assembled into one result: the text, every tool call with its parsed input, usage and cost. - **The tool loop.** Your tool functions are called and their results sent back until the model stops calling tools. - **Dropped connections.** Every submission carries an idempotency key. If the connection drops, the turn carries on server-side and the SDK rebuilds its result from the chat's history. - **Retries.** Rate limits and transient failures are retried where a retry is safe, honouring `Retry-After`. - **Chats by your own id.** Address a chat by a phone number or another system's conversation id, with no mapping to store: the first message creates the chat, one request per message. See "External ids" in the [quickstart](https://twigg.ai/docs/quickstart.md). Every SDK takes your API key from `TWIGG_API_KEY`, or from the client's options. ## TypeScript `@twigg/sdk` 0.2.0 on npm. No runtime dependencies; runs on Node 22+, Deno, Bun and edge runtimes. ```bash npm install @twigg/sdk ``` ```ts import { Twigg } from "@twigg/sdk"; const twigg = new Twigg({ model: "claude-opus-5" }); // reads TWIGG_API_KEY const chat = await twigg.chats.open({ namespace: "acme/user-99", title: "Billing" }); const reply = await chat.send("What changed in billing?"); console.log(reply.text, reply.cost); ``` `model` on the client is the default for every turn; a chat's defaults or a single call can override it. ### Quick reference ```ts import { Twigg, tool } from "@twigg/sdk"; const twigg = new Twigg({ model: "gpt-6-luna" }); // key from TWIGG_API_KEY const models = await twigg.models.list(); // names to pass as `model` const chat = await twigg.chats.open({ namespace: "acme/user-99" }); // new chat const again = twigg.chat(chatId); // existing chat, no request const byPhone = twigg.chatByExternalId("+447700900123"); // your own id; made on first send const reply = await chat.send("Hi"); // .text .cost .usage .toolCalls .stopReason const streamed = await chat.stream("Hi").on("text", (d) => process.stdout.write(d)).final(); const outcome = await chat.runTools("Hi", { tools: [myTool] }).final(); // the tool loop for await (const c of twigg.chats.list({ namespace: "acme" })) {} // every chat, newest first for await (const p of chat.history({ after_ordinal: -1 })) {} // transcript, oldest first const acme = twigg.namespace("acme"); // pins the namespace await acme.config.instructions.publish({ mode: "append", body: "Answer briefly." }); const run = await twigg.runs.waitForSettlement(reply.runId); // the final cost ``` ### Tools ```ts import { tool } from "@twigg/sdk"; import { z } from "zod"; const readFile = tool({ name: "read_file", description: "Read a file from the repository", input: z.object({ path: z.string() }), run: ({ path }) => fs.readFile(path, "utf8"), }); const outcome = await chat.runTools("Summarise the billing module", { tools: [readFile] }).final(); console.log(outcome.result.text, outcome.turns.length); ``` ## Python `twigg` 0.2.0 on PyPI. Sync and async (`AsyncTwigg`), fully typed, built on `httpx` and `pydantic`. Python 3.10+. ```bash pip install twigg ``` ```python from twigg import Twigg twigg = Twigg(model="claude-opus-5") # reads TWIGG_API_KEY chat = twigg.chats.open(namespace="acme/user-99", title="Billing") reply = chat.send("What changed in billing?") print(reply.text) ``` ### Quick reference ```python from twigg import Twigg, tool twigg = Twigg(model="gpt-6-luna") # key from TWIGG_API_KEY models = twigg.models.list() # names to pass as `model` chat = twigg.chats.open(namespace="acme/user-99") # new chat again = twigg.chat(chat_id) # existing chat, no request by_phone = twigg.chat_by_external_id("+447700900123") # your own id; made on first send reply = chat.send("Hi") # .text .cost .usage .tool_calls .stop_reason with chat.stream("Hi") as stream: for text in stream.text_stream: print(text, end="") outcome = chat.run_tools("Hi", tools=[my_tool]).final() # the tool loop chats = list(twigg.chats.list(namespace="acme")) # every chat, newest first transcript = list(chat.history(after_ordinal=-1)) # oldest first acme = twigg.namespace("acme") # pins the namespace acme.config.instructions.publish(mode="append", body="Answer briefly.") run = twigg.runs.wait_for_settlement(reply.run_id) # the final cost ``` ### Tools ```python from twigg import tool @tool def read_file(path: str) -> str: """Read a file from the repo.""" return open(path).read() outcome = chat.run_tools("Summarise the billing module", tools=[read_file]).final() print(outcome.stop_reason, outcome.result.text) ``` ## CLI `@twigg/cli` 0.2.0 on npm. Declare system instructions, tools, context budgets and retention for your organisation and its namespaces in one file, review a plan, and apply it from CI. ```bash npm install --save-dev @twigg/cli export TWIGG_API_KEY=tw_... npx twigg config pull # start from what is configured today npx twigg config plan # what would change npx twigg config apply # make it so ``` The file is `twigg.config.yaml`, `twigg.config.json` or `twigg.config.ts`: ```yaml # yaml-language-server: $schema=https://twigg.ai/schemas/config.v1.json version: 1 global: instructions: { $file: prompts/global.md } namespaces: acme: instructions: mode: append body: { $file: prompts/acme.md } tools: definitions: - $file: tools/read_file.json budgets: default: { version: 3, headroom: 0.2, history_mode: summarize } claude-opus-5: { version: 3, headroom: 0.15, max_history_tokens: 200000 } acme/support: instructions: mode: replace body: { $file: prompts/support.md } tools: null ``` The same settings can be managed through the configuration API or the console; see [Context control](https://twigg.ai/docs/context.md).