Skip to main content
Magic functions are stateless, AI-implemented functions; each call is independent. Implement magic functions by calling the magic() function within your async functions.

Magic function

The magic function enables AI-powered function implementation within your TypeScript code. Function bodies should call magic with a descriptive prompt.
async function magic<T>(
  prompt: string,
  scope?: object,
  config?: {
    model?: 'openai:gpt-3.5-turbo'
          | 'openai:gpt-4o'
          | 'openai:gpt-4.1'
          | 'openai:gpt-5'
          | 'anthropic:claude-sonnet-4'
          | 'anthropic:claude-opus-4.1'
          | 'anthropic:claude-sonnet-4.5',
    premise?: string,
    system?: string,
    maxTokens?: number,
    listener?: (iid: string, chunk: Chunk) => void,
  },
): Promise<T>;
Parameters
prompt
string
The task description that guides the AI’s execution. This describes what the function should do.
scope
object
Optional. An object containing runtime resources that are available during execution. Resources in scope may be functions, methods, objects, types or any other value.
config
object
Optional configuration object for the magic function execution.
config.premise
string
Additional information made available in the agent’s system prompt.
config.system
string
Alternatively to providing premise, you can set explicitly set agent’s system prompt.
config.model
string
The model used to execute the magic function.One of:
  • openai:gpt-3.5-turbo
  • openai:gpt-4o
  • openai:gpt-4.1
  • openai:gpt-5
  • anthropic:claude-sonnet-4
  • anthropic:claude-opus-4.1
  • anthropic:claude-sonnet-4.5
Defaults to openai:gpt-4.1.
config.maxTokens
number
The maximum number of tokens that the agent can generate during one round of inference.Defaults to 2048.
config.listener
(iid: string, chunk: Chunk) => void
A callback for streaming model generation. Each streamed text chunk for each magic function call, determined by a unique invocation ID, will route to this callback.
Returns
Promise<T>
Returns a promise that resolves to the result of specified or inferred return type T.