If you already use MiniMax Code through the CLI, an editor integration, or headless Stream events:
Threads:
Thread options are passed to the CLI on every Turn of that Thread:
Concurrent
mcode exec, you can also control it from application code with the TypeScript SDK.Use the SDK when you need to:- Automate fixes, reviews, or code generation in CI/CD;
- Embed MiniMax Code in internal tools or your own agents;
- Orchestrate multi-turn Sessions from code instead of hand-written
mcode execpipelines.
mcode exec --output-format stream-json: one run() / runStreamed() call is one CLI process and one Turn. Phase 1 (0.x) may change APIs between minor versions until 1.0.Related pages: Headless and CI, Python SDK. Package: npm @minimax-ai/code-sdk.Requirements
The SDK and CLI ship separately. Upgrading the SDK does not require a CLI upgrade as long as the CLI meets
MIN_CLI_VERSION. Sign in or configure a working API-key provider in the data directory before the first run.Installation
@minimax-ai/code is an optional peer dependency and runs install scripts for native modules. npm 11 rejects --allow-scripts for project-scoped installs (EALLOWSCRIPTS), so approve them in the project’s package.json first:Quick start
Create a client, start a Thread, and run a prompt:CLI resolution
The SDK does not hard-depend on the CLI. Lookup order:- The
binconstructor option (executable, JS entry, or command name onPATH); - An installed
@minimax-ai/codenext to the SDK (optional peer); mcodeonPATH.
resolveFromCwd defaults to false. Set it to true to also search the host working directory after the SDK-local package and before PATH. Leave it off for untrusted checkouts (for example CI pull-request jobs); pass bin, or install the CLI next to the SDK or on PATH.Before the first run, each MCode instance runs mcode --version once and caches the result. A CLI older than 0.6.3 fails with MCodeVersionError. Only major.minor.patch is compared, so 0.6.3-preview.N satisfies the minimum.new MCode(options?)
env replaces the environment entirely. Passing only custom variables drops PATH, HOME, and similar values. To add variables, spread process.env:Threads: startThread / resumeThread
Thread options are passed to the CLI on every Turn of that Thread:Notes:
mode: "lightweight"is only valid for a new Thread; passing it toresumeThreadthrowsMCodeInvocationErrorimmediately;- There is no
askpermission for headless runs. MiniMax Code’ssmart/full/offpolicies are not equivalent to Codex OS sandbox modes, and the SDK does not map between them; thread.idbecomes available once the first Turn emits its first event, even if that Turn fails.
thread.run / thread.runStreamed
Behavior:
run()resolves only when the status issucceeded, with aTurn(finalResponse, normalizeditems,usage,model,sessionId,turnId,durationMs, and the fullresult). Every other outcome rejects;runStreamed()yields CLI events unchanged.turn.failedis an ordinary event; the iterator throws only for process or protocol failures. Breaking out of the loop cancels the Turn;- A Thread runs one Turn at a time. A concurrent call throws
ThreadBusyError; Turns are not queued.
Events and items
Event types match the CLI:exec.started, session.started / session.resumed, turn.started, item.started / item.updated / item.completed, turn.completed, turn.failed, and exec.completed. Every stdout line must be JSON with schemaVersion === 1; anything else is a protocol error.Turn.items are SDK-normalized items: agent_message, reasoning, and tool_call. Items merge by item.id in first-appearance order. Items without item.completed when the Turn ends are marked incomplete: true.usage.totalTokens = inputTokens + outputTokens and excludes cacheReadTokens / cacheWriteTokens; reasoningTokens may overlap outputTokens.Errors
Every error extendsMCodeError and carries exitCode, signal, and the last 64 KiB of CLI stderr (stderr, diagnostics only).Common classes:The SDK never retries automatically, to avoid replaying writes with unknown outcomes. When an error includes
sessionId, call resumeThread(error.sessionId) to continue.Cancellation and timeouts
- When
signalaborts, the SDK sendsSIGTERM. The CLI cancels and shuts down (up to about 60 seconds); after that budget the SDK sendsSIGKILL; timeoutMsis enforced by the CLI after access checks, session resolution, and model selection. The SDK starts its timer at spawn and force-stops attimeoutMs + 90s;- On a normal host exit, the SDK sends
SIGTERMto any running CLI and deletes temporary system-prompt directories. A host killed by a signal does not emitexit, so child processes may remain. Phase 1 has noclose()/dispose(); - On Windows, Node.js cannot deliver a catchable signal to a child process, so
SIGTERMis a hard kill.
exec processes that share one data directory have had limited testing. Prefer a separate dataDir per SDK instance (or concurrent task), with authentication configured in that directory beforehand.Not available in Phase 1
outputSchema(structured-output constraints);- Approval callbacks, steering a running Turn, continuing after an interrupt, and forking.