Function generateText

  • Stateless text generation with optional multi-step tool calling.

    Creates a temporary provider manager, executes one or more LLM completion steps (each tool-call round trip counts as one step), and returns the final assembled result. Provider credentials are resolved from environment variables unless overridden in opts.

    When planning is enabled, an upfront LLM call produces a step-by-step plan that is then injected into the system prompt for the tool loop.

    Parameters

    • opts: GenerateTextOptions

      Generation options including model, prompt/messages, and optional tools.

    Returns Promise<GenerateTextResult>

    A promise that resolves to the final text, token usage, tool call log, and finish reason.

    Example

    const result = await generateText({
    model: 'openai:gpt-4o',
    prompt: 'Summarise the history of the Roman Empire in two sentences.',
    });
    console.log(result.text);