Skip to content

defineAgent

ts
function defineAgent<TName, TTools, TSchema, TInterrupts, TInput, TResult, TProduces>(agent): DefinedAgent<TName, TTools, TSchema, TInterrupts, TInput, TResult, TProduces>;

Defined in: packages/ai/src/activities/chat/agents/define-agent.ts:180

Define a named child agent. Pass the same object to chat({ subagents }). Pass the agents array to useChat({ subagents }) when you render parts yourself. The hook uses it for types only. It does not call run.

Type Parameters

TName

TName extends string

TTools

TTools extends readonly SubagentTool[] = readonly []

TSchema

TSchema extends SchemaInput | undefined = undefined

TInterrupts

TInterrupts extends readonly InterruptDefinition<any, any, any, any, any>[] = readonly []

TInput

TInput extends SchemaInput | undefined = undefined

TResult

TResult = unknown

TProduces

TProduces extends AgentProduces | undefined = undefined

Parameters

agent

DefinedAgent<TName, TTools, TSchema, TInterrupts, TInput, TResult, TProduces>

Returns

DefinedAgent<TName, TTools, TSchema, TInterrupts, TInput, TResult, TProduces>

Example

ts
const researcher = defineAgent({
  name: 'researcher',
  description: 'Looks up facts',
  run: (ctx) =>
    chat({
      adapter: openaiText('gpt-6.1-sol'),
      messages: ctx.messages,
      threadId: ctx.threadId,
      runId: ctx.runId,
      parentRunId: ctx.parentRunId,
      subagentRunId: ctx.subagentRunId,
      resume: ctx.resume,
    }),
})