.agents/skills/cline-sdk/references/agent/api.md
import { Agent } from "@cline/sdk"
const agent = new Agent(config: AgentRuntimeConfig)
Also available via factory function:
import { createAgent } from "@cline/sdk"
const agent = createAgent(config)
Two config forms exist as a discriminated union:
interface AgentRuntimeConfigWithProvider {
providerId: string // e.g. "anthropic", "openai", "gemini"
modelId: string // e.g. "claude-sonnet-4-6", "gpt-5.5"
apiKey?: string // provider API key
baseUrl?: string // custom endpoint
headers?: Record<string, string>
systemPrompt?: string
tools?: AgentTool[]
initialMessages?: AgentMessage[]
toolPolicies?: Record<string, ToolPolicy>
hooks?: Partial<AgentRuntimeHooks>
plugins?: AgentPlugin[]
}
interface AgentRuntimeConfigWithModel {
model: AgentModel // pre-built model from gateway
systemPrompt?: string
tools?: AgentTool[]
initialMessages?: AgentMessage[]
toolPolicies?: Record<string, ToolPolicy>
hooks?: Partial<AgentRuntimeHooks>
plugins?: AgentPlugin[]
}
Note: there is no top-level onEvent field on AgentRuntimeConfig. For event streaming, use agent.subscribe() or hooks.onEvent (see AgentRuntimeHooks below).
Start the agent with user input. Returns when the agent loop completes.
const result: AgentRunResult = await agent.run("Build a REST API")
Input can be a string, an AgentMessage, or an array of AgentMessage[].
Continue an existing conversation with optional new input.
const result = await agent.continue("Now add authentication")
Cancel the currently active run.
agent.abort("User cancelled")
Register a listener for streaming events.
const unsubscribe = agent.subscribe((event: AgentRuntimeEvent) => {
// handle event
})
// Later: stop listening
unsubscribe()
Get the current runtime state including message history.
const state: AgentRuntimeStateSnapshot = agent.snapshot()
Replace the agent's message history.
agent.restore(previousMessages)
Boolean property indicating whether run() has been called at least once.
if (agent.hasRun) {
await agent.continue(input)
} else {
await agent.run(input)
}
Returned by run() and continue().
interface AgentRunResult {
agentId: string
agentRole?: string
runId: string
status: "completed" | "aborted" | "failed"
iterations: number
outputText: string
messages: readonly AgentMessage[]
usage: AgentUsage
error?: Error
}
"completed" - Agent finished normally"aborted" - Cancelled via abort()"failed" - Unrecoverable errorinterface AgentMessage {
id: string
role: "user" | "assistant" | "tool"
content: AgentMessagePart[]
createdAt: number
metadata?: Record<string, unknown>
modelInfo?: { id: string; provider: string; family?: string }
metrics?: {
inputTokens: number
outputTokens: number
cacheReadTokens?: number
cacheWriteTokens?: number
cost?: number
}
}
interface AgentUsage {
inputTokens: number
outputTokens: number
cacheReadTokens: number
cacheWriteTokens: number
totalInputTokens: number
totalOutputTokens: number
totalCost?: number
}
interface AgentRuntimeHooks {
beforeRun?(context): AgentStopControl | undefined
afterRun?(context): void
beforeModel?(context): AgentBeforeModelResult | undefined
afterModel?(context): AgentStopControl | undefined
beforeTool?(context): AgentBeforeToolResult | undefined
afterTool?(context): AgentAfterToolResult | undefined
onEvent?(event: AgentRuntimeEvent): void | Promise<void>
}
Hooks can intercept and modify behavior at each stage. Return a stop control from beforeRun, afterModel, or beforeTool to halt the agent loop.
hooks.onEvent receives the same AgentRuntimeEvent types as agent.subscribe(), but hook callbacks are awaited (can be async), while subscribe() listeners are called synchronously. Use subscribe() for UI streaming and hooks.onEvent for async side effects like logging to an external service.
interface AgentRuntimeStateSnapshot {
messages: readonly AgentMessage[]
usage: AgentUsage
iterations: number
status: string
}
Lower-level factory that returns the same Agent class:
import { createAgentRuntime } from "@cline/sdk"
const runtime = createAgentRuntime(config)
REFERENCE.md - Overview and quick startpatterns.md - Common patterns../tools/REFERENCE.md - Tool creation../events/REFERENCE.md - Event types../providers/REFERENCE.md - Provider setup