Skip to main content

Overview

LlamaIndex.TS provides a unified interface for working with Large Language Models (LLMs) from various providers. All LLMs implement the BaseLLM interface, allowing you to switch between providers with minimal code changes.

BaseLLM Interface

The BaseLLM abstract class from @llamaindex/core/llms provides the foundation for all LLM implementations:

LLM Metadata

Every LLM instance exposes metadata about its configuration:

Chat vs Completion

LlamaIndex.TS supports two interaction modes:

Chat API

The chat API uses message-based conversations with role-aware messages:
Message roles:
  • system - System instructions that guide the model’s behavior
  • user - User messages/queries
  • assistant - Model responses
  • developer - Developer messages (for some providers)
  • memory - Memory/context messages

Completion API

The completion API is simpler, using direct text prompts:
The complete method internally converts to chat messages, so both APIs use the same underlying implementation.

Streaming

All LLMs support streaming responses for real-time output:

Streaming Chat

Streaming Completion

Function Calling

Modern LLMs support function calling (also called tool calling) to interact with external tools:

Using Tools

Structured Output with exec()

The exec() method provides an easier way to handle tool calling and structured output:

Streaming with Tools

Not all providers support function calling. Check the provider documentation for compatibility.

Configuration Options

All LLMs support common configuration options:

Provider-Specific Options

Some providers offer additional options via additionalChatOptions:

Multi-Modal Support

Many LLMs support images, audio, and other modalities:

Images

Files (PDFs, etc.)

Examples

OpenAI

Anthropic

Ollama (Local Models)

Google Gemini

Best Practices

  • 0.0-0.3: Deterministic, factual tasks (extraction, classification)
  • 0.4-0.7: Balanced (general chat, Q&A)
  • 0.8-1.0: Creative tasks (writing, brainstorming)
Always use streaming for user-facing applications to provide immediate feedback:
LlamaIndex.TS automatically detects these environment variables.

Next Steps

Embeddings

Learn about embedding models for semantic search

Providers

Explore all available LLM providers