Example Gallery

Explore 70 working examples covering all LLM4S features.

Table of contents

  1. Quick Navigation
  2. Playground
    1. CliPlaygroundDemo
  3. Basic Examples
    1. BasicLLMCallingExample
    2. StreamingExample
    3. AdvancedStreamingExample
    4. BasicLLMCallingWithTrace
    5. EnhancedTracingExample
    6. LangfuseSampleTraceRunner
    7. OllamaExample
    8. OllamaStreamingExample
    9. AgentLLMCallingExample
    10. ProviderFallbackExample
  4. Agent Examples
    1. SingleStepAgentExample
    2. MultiStepAgentExample
    3. MultiTurnConversationExample
    4. LongConversationExample
    5. ConversationPersistenceExample
    6. MCPAgentExample
    7. BuiltinToolsAgentExample
  5. Tool Examples
    1. WeatherToolExample
    2. MultiToolExample
    3. ErrorMessageDemonstration
    4. ImprovedErrorMessageDemo
    5. BuiltinToolsExample
    6. ParallelToolExecutionExample
  6. Guardrails Examples
    1. BasicInputValidationExample
    2. CustomGuardrailExample
    3. CompositeGuardrailExample
    4. JSONOutputValidationExample
    5. MultiTurnToneValidationExample
    6. FactualityGuardrailExample
    7. LLMJudgeGuardrailExample
  7. Handoff Examples
    1. SimpleTriageHandoffExample
    2. MathSpecialistHandoffExample
    3. ContextPreservationExample
  8. Memory Examples
    1. BasicMemoryExample
    2. ConversationMemoryExample
    3. MemoryWithAgentExample
    4. SQLiteMemoryExample
    5. VectorMemoryExample
    6. DocumentQAExample (RAG)
  9. Context Management
    1. ContextPipelineExample
  10. Embeddings
    1. EmbeddingExample
    2. S3LoaderExample
  11. RAG in a Box
  12. MCP Examples
    1. MCPToolExample
  13. Streaming Examples
    1. BasicStreamingExample
    2. StreamingWithProgressExample
    3. Agent event streams
  14. Reasoning Examples
    1. ReasoningModesExample
  15. Model Examples
    1. ModelMetadataExample
    2. CostTrackingExample
    3. MultiProviderComparisonExample
  16. Other Examples
    1. Interactive Assistant
    2. Speech
    3. Actions
  17. Running Examples
    1. Basic Run Command
    2. Choosing a Provider
    3. Browse Source
  18. Learning Paths
    1. Beginner Path
    2. Intermediate Path
    3. Advanced Path
  19. Next Steps

Quick Navigation

Category Count Description
Playground 1 Offline demo — no API key needed
Basic Examples 9 Getting started, streaming, tracing
Agent Examples 8 Multi-turn agents, persistence, built-in tools
Tool Examples 7 Tool calling, built-in tools, parallel execution
Guardrails Examples 7 Input/output validation, LLM-as-Judge
Handoff Examples 3 Agent-to-agent delegation
Memory Examples 6 Short/long-term memory, vector search, RAG
Streaming Examples 9 Real-time responses, agent event streams
Reasoning Examples 1 Extended thinking modes
Context Management 8 Token windows, compression
Embeddings 5 Vector search, RAG
RAG in a Box - Production RAG server (external project)
MCP Examples 3 Model Context Protocol
Model Examples 1 Model metadata and capabilities
Other Examples 8 Speech, actions, utilities

Playground

Location: modules/samples/src/main/scala/org/llm4s/samples/playground/

CliPlaygroundDemo

File: CliPlaygroundDemo.scala

Fully offline playground demo — no API key or network access required. Replays 6 pre-recorded scenarios with ANSI-coloured output, then drops into an interactive prompt loop so you can type your own queries and see simulated traced responses in real time.

1
sbt "samples/runMain org.llm4s.samples.playground.CliPlaygroundDemo"

What it demonstrates:

  • Simple text completion
  • Multi-turn conversation
  • Tool calling
  • Agent pipeline (multi-step orchestration)
  • Error handling and recovery
  • Token usage awareness
  • Interactive REPL-style prompt loop (Scenario 7)

Perfect for: First-time users and workshop attendees exploring LLM4S before wiring up a real provider

View source →


Basic Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/basic/

BasicLLMCallingExample

File: BasicLLMCallingExample.scala

Simple multi-turn conversations demonstrating system, user, and assistant messages.

1
sbt "samples/runMain org.llm4s.samples.basic.BasicLLMCallingExample"

What it demonstrates:

  • Creating a conversation with multiple message types
  • System message for setting assistant behavior
  • Multi-turn context with AssistantMessage
  • Token usage tracking
  • Error handling with Result types

View source →


StreamingExample

File: StreamingExample.scala

Compare streaming vs non-streaming responses with performance metrics.

1
sbt "samples/runMain org.llm4s.samples.basic.StreamingExample"

What it demonstrates:

  • Real-time token-by-token output
  • Performance comparison (streaming vs batch)
  • Chunk processing
  • Measuring response times

View source →


AdvancedStreamingExample

File: AdvancedStreamingExample.scala

More complex streaming patterns with error handling and state management.

1
sbt "samples/runMain org.llm4s.samples.basic.AdvancedStreamingExample"

What it demonstrates:

  • Advanced error handling during streaming
  • State management across chunks
  • Progress tracking
  • Stream termination handling

BasicLLMCallingWithTrace

File: BasicLLMCallingWithTrace.scala

Basic LLM calls with integrated tracing for observability.

1
2
3
# Configure tracing
export TRACING_MODE=console
sbt "samples/runMain org.llm4s.samples.basic.BasicLLMCallingWithTrace"

What it demonstrates:

  • Console tracing integration
  • Token usage tracking
  • Request/response logging
  • Performance metrics

EnhancedTracingExample

File: EnhancedTracingExample.scala

Advanced tracing with detailed token usage and agent state tracking.

1
2
export TRACING_MODE=console
sbt "samples/runMain org.llm4s.samples.basic.EnhancedTracingExample"

What it demonstrates:

  • Detailed trace information
  • Agent state tracking
  • Token usage analysis
  • Multi-level tracing

LangfuseSampleTraceRunner

File: LangfuseSampleTraceRunner.scala

Production-grade tracing with Langfuse backend.

1
2
3
4
export TRACING_MODE=langfuse
export LANGFUSE_PUBLIC_KEY=pk-lf-...
export LANGFUSE_SECRET_KEY=sk-lf-...
sbt "samples/runMain org.llm4s.samples.basic.LangfuseSampleTraceRunner"

What it demonstrates:

  • Langfuse integration
  • Production observability
  • Trace persistence
  • Analytics dashboard

Learn more about Langfuse →


OllamaExample

File: OllamaExample.scala

Using local Ollama models instead of cloud providers.

1
2
3
4
5
6
7
8
# Start Ollama
ollama serve &
ollama pull llama2

# Run example - the samples' default section is ollama-local;
# OLLAMA_MODEL and OLLAMA_BASE_URL are bound by the samples' application.conf
export OLLAMA_MODEL=llama2
sbt "samples/runMain org.llm4s.samples.basic.OllamaExample"

What it demonstrates:

  • Local model execution
  • No API key required
  • Provider flexibility
  • Cost-free development

OllamaStreamingExample

File: OllamaStreamingExample.scala

Streaming responses with local Ollama models.

1
2
export OLLAMA_MODEL=llama2   # read by the samples' application.conf
sbt "samples/runMain org.llm4s.samples.basic.OllamaStreamingExample"

To enable raw provider exchange logging and write timestamped JSONL files to a specific directory:

1
2
export OLLAMA_MODEL=llama2
sbt "samples/runMain org.llm4s.samples.basic.OllamaStreamingExample /tmp/my-provider-exchanges"

What it demonstrates:

  • Local streaming
  • Ollama-specific features
  • Performance characteristics
  • Raw provider exchange logging to JSONL

AgentLLMCallingExample

File: AgentLLMCallingExample.scala

Making LLM calls from within an agent context.

1
sbt "samples/runMain org.llm4s.samples.basic.AgentLLMCallingExample"

What it demonstrates:

  • Agent-based LLM calls
  • Context management
  • Agent state tracking

ProviderFallbackExample

File: ProviderFallbackExample.scala

LLM calls with automatic provider fallback for reliability.

1
sbt "samples/runMain org.llm4s.samples.basic.ProviderFallbackExample"

What it demonstrates:

  • Multiple provider configurations
  • Automatic fallback on failure
  • Enhanced reliability

Agent Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/agent/

SingleStepAgentExample

File: SingleStepAgentExample.scala

A plain agent run with a step limit, printing the messages the run produced. (For step-level events, see the streaming examples.)

1
sbt "samples/runMain org.llm4s.samples.agent.SingleStepAgentExample"

What it demonstrates:

  • Agent.builder(...).withMaxSteps(n)
  • Tool calling in a run
  • Reading AgentResult.messages

Perfect for: Understanding what a run produces

View source →


MultiStepAgentExample

File: MultiStepAgentExample.scala

Complete agent execution from start to finish with automatic tool calling.

1
sbt "samples/runMain org.llm4s.samples.agent.MultiStepAgentExample"

What it demonstrates:

  • Automatic agent execution
  • Tool calling loop
  • Conversation completion
  • Final response generation

Perfect for: Production-ready agent patterns


MultiTurnConversationExample

File: MultiTurnConversationExample.scala

Functional, immutable multi-turn conversation API (Phase 1.1).

1
sbt "samples/runMain org.llm4s.samples.agent.MultiTurnConversationExample"

What it demonstrates:

  • continueConversation() pattern
  • Immutable results, a conversation carried by its thread
  • No var or mutation
  • Clean functional style

Key code:

1
val result2 = agent.continueConversation(result1, "Follow-up question")

View source →


LongConversationExample

File: LongConversationExample.scala

Long conversations with automatic context window pruning.

1
sbt "samples/runMain org.llm4s.samples.agent.LongConversationExample"

What it demonstrates:

  • runMultiTurn() helper method
  • Automatic token management
  • Context window pruning strategies
  • Memory-efficient conversations

Key code:

1
2
3
4
val config = ContextWindowConfig(
  maxMessages = Some(20),
  pruningStrategy = PruningStrategy.OldestFirst
)

View source →


ConversationPersistenceExample

File: ConversationPersistenceExample.scala

Save a run’s messages and load them as the history of a new thread.

1
sbt "samples/runMain org.llm4s.samples.agent.ConversationPersistenceExample"

What it demonstrates:

  • Saving a completed run’s messages to disk
  • Loading them as history on a new thread
  • JSON serialization
  • Session management

Key code:

1
2
Files.writeString(path, write(result.messages))
agent.run(newThreadId, "Next question", RunConfig(), history = loaded)

View source →


MCPAgentExample

File: MCPAgentExample.scala

Agents with Model Context Protocol (MCP) tool integration.

1
sbt "samples/runMain org.llm4s.samples.agent.MCPAgentExample"

What it demonstrates:

  • MCP tool integration in agents
  • External tool servers
  • Protocol fallback handling

BuiltinToolsAgentExample

File: BuiltinToolsAgentExample.scala

Agent using built-in tools (DateTime, Calculator, web search, etc.).

1
sbt "samples/runMain org.llm4s.samples.agent.BuiltinToolsAgentExample"

View source →


Tool Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/toolapi/

WeatherToolExample

File: WeatherToolExample.scala

Simple tool definition and execution.

1
sbt "samples/runMain org.llm4s.samples.toolapi.WeatherToolExample"

What it demonstrates:

  • Basic tool creation with ToolFunction
  • Parameter schema definition
  • Tool execution
  • Return value handling

Key code:

1
2
3
4
5
val weatherTool = ToolFunction(
  name = "get_weather",
  description = "Get current weather for a location",
  function = getWeather _
)

View source →


MultiToolExample

File: MultiToolExample.scala

Multiple tools with different parameter types.

1
sbt "samples/runMain org.llm4s.samples.toolapi.MultiToolExample"

What it demonstrates:

  • Calculator tool
  • Search tool
  • Multiple tools in one registry
  • Tool precedence and selection

Key code:

1
val tools = new ToolRegistry(Seq(calculatorTool, searchTool))

View source →


ErrorMessageDemonstration

File: ErrorMessageDemonstration.scala

Error handling in tool execution with helpful messages.

1
sbt "samples/runMain org.llm4s.samples.toolapi.ErrorMessageDemonstration"

What it demonstrates:

  • Tool validation errors
  • Helpful error messages
  • Error recovery patterns

ImprovedErrorMessageDemo

File: ImprovedErrorMessageDemo.scala

Enhanced error reporting for better debugging.

1
sbt "samples/runMain org.llm4s.samples.toolapi.ImprovedErrorMessageDemo"

What it demonstrates:

  • Detailed error context
  • Stack traces
  • Debugging information

BuiltinToolsExample

File: BuiltinToolsExample.scala

Using the built-in tools library (DateTime, Calculator, UUID, JSON, HTTP, etc.).

1
sbt "samples/runMain org.llm4s.samples.toolapi.BuiltinToolsExample"

View source →


ParallelToolExecutionExample

File: ParallelToolExecutionExample.scala

Executing multiple tool calls in parallel with different strategies.

1
sbt "samples/runMain org.llm4s.samples.toolapi.ParallelToolExecutionExample"

View source →


Guardrails Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/guardrails/

BasicInputValidationExample

File: BasicInputValidationExample.scala

Basic input validation with built-in guardrails.

1
sbt "samples/runMain org.llm4s.samples.guardrails.BasicInputValidationExample"

What it demonstrates:

  • LengthCheck guardrail for input size validation
  • ProfanityFilter for content filtering
  • Declarative validation before agent processing
  • Clear error messages for validation failures

View source →


CustomGuardrailExample

File: CustomGuardrailExample.scala

Build custom guardrails for application-specific validation.

1
sbt "samples/runMain org.llm4s.samples.guardrails.CustomGuardrailExample"

What it demonstrates:

  • Implementing custom InputGuardrail trait
  • Keyword requirement validation
  • Reusable validation logic
  • Testing validation success and failure cases

Key code:

1
2
3
4
5
class KeywordRequirementGuardrail(requiredKeywords: Set[String]) extends InputGuardrail {
  def validate(value: String): Result[String] = {
    // Custom validation logic
  }
}

View source →


CompositeGuardrailExample

File: CompositeGuardrailExample.scala

Combine multiple guardrails with different composition strategies.

1
sbt "samples/runMain org.llm4s.samples.guardrails.CompositeGuardrailExample"

What it demonstrates:

  • Sequential composition (all must pass in order)
  • All composition (all must pass, run in parallel)
  • Any composition (at least one must pass)
  • Error accumulation and reporting

Key code:

1
2
3
4
5
val allGuardrails = CompositeGuardrail.all(Seq(
  LengthCheck(min = 10, max = 1000),
  ProfanityFilter(),
  customGuardrail
))

View source →


JSONOutputValidationExample

File: JSONOutputValidationExample.scala

Validate LLM outputs are valid JSON.

1
sbt "samples/runMain org.llm4s.samples.guardrails.JSONOutputValidationExample"

What it demonstrates:

  • Output guardrails (run after LLM response)
  • JSON format validation
  • Structured output enforcement
  • Integration with agent workflows

View source →


MultiTurnToneValidationExample

File: MultiTurnToneValidationExample.scala

Validate conversational tone across multiple turns.

1
sbt "samples/runMain org.llm4s.samples.guardrails.MultiTurnToneValidationExample"

What it demonstrates:

  • ToneValidator for output validation
  • Maintaining consistent tone
  • Multi-turn conversation with guardrails
  • Professional/friendly tone enforcement

View source →


FactualityGuardrailExample

File: FactualityGuardrailExample.scala

LLM-as-Judge guardrail for validating factual accuracy of responses.

1
sbt "samples/runMain org.llm4s.samples.guardrails.FactualityGuardrailExample"

View source →


LLMJudgeGuardrailExample

File: LLMJudgeGuardrailExample.scala

Using LLM-as-Judge for content safety, quality, and tone validation.

1
sbt "samples/runMain org.llm4s.samples.guardrails.LLMJudgeGuardrailExample"

View source →


Handoff Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/handoff/

SimpleTriageHandoffExample

File: SimpleTriageHandoffExample.scala

Basic agent-to-agent handoff for routing queries to specialists.

1
sbt "samples/runMain org.llm4s.samples.handoff.SimpleTriageHandoffExample"

View source →


MathSpecialistHandoffExample

File: MathSpecialistHandoffExample.scala

Handoff to a math specialist agent for complex calculations.

1
sbt "samples/runMain org.llm4s.samples.handoff.MathSpecialistHandoffExample"

View source →


ContextPreservationExample

File: ContextPreservationExample.scala

Preserving conversation context when handing off between agents.

1
sbt "samples/runMain org.llm4s.samples.handoff.ContextPreservationExample"

View source →


Memory Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/memory/

BasicMemoryExample

File: BasicMemoryExample.scala

Getting started with the memory system for recording facts and retrieving context.

1
sbt "samples/runMain org.llm4s.samples.memory.BasicMemoryExample"

View source →


ConversationMemoryExample

File: ConversationMemoryExample.scala

Using memory to maintain context across conversation turns.

1
sbt "samples/runMain org.llm4s.samples.memory.ConversationMemoryExample"

View source →


MemoryWithAgentExample

File: MemoryWithAgentExample.scala

Integrating memory with agent workflows for personalized responses.

1
sbt "samples/runMain org.llm4s.samples.memory.MemoryWithAgentExample"

View source →


SQLiteMemoryExample

File: SQLiteMemoryExample.scala

Persistent memory storage using SQLite backend.

1
sbt "samples/runMain org.llm4s.samples.memory.SQLiteMemoryExample"

View source →


VectorMemoryExample

File: VectorMemoryExample.scala

Semantic memory search using embeddings and vector store.

1
sbt "samples/runMain org.llm4s.samples.memory.VectorMemoryExample"

View source →


DocumentQAExample (RAG)

File: DocumentQAExample.scala

Complete RAG (Retrieval-Augmented Generation) pipeline demonstrating document Q&A with semantic search.

1
2
3
4
5
6
7
8
9
# With mock embeddings (no API key needed for embeddings), answering with
# the default ollama-local section - or select another, see "Running Examples"
sbt "samples/runMain org.llm4s.samples.rag.DocumentQAExample"

# With real OpenAI embeddings: EMBEDDING_MODEL is bound by llm4s-core, and
# OPENAI_API_KEY by llm4s-openai (the key OpenAI chat sections use too)
export EMBEDDING_MODEL=openai/text-embedding-3-small
export OPENAI_API_KEY=sk-...
sbt "samples/runMain org.llm4s.samples.rag.DocumentQAExample"

What it demonstrates:

  • Document loading and text extraction
  • Text chunking with configurable size and overlap
  • Embedding generation (mock or real via OpenAI/VoyageAI)
  • Vector storage with SQLite backend
  • Semantic similarity search
  • RAG prompt construction with context
  • Answer generation with source citations

Key code:

1
2
3
4
5
6
7
// Ingest documents
val chunks = ChunkingUtils.chunkText(text, chunkSize = 800, overlap = 150)
store.store(Memory.fromKnowledge(chunk, source = fileName))

// Query with semantic search
val results = store.search(query, topK = 4)
val answer = client.complete(buildRAGPrompt(query, results))

View source →


Context Management

Location: modules/samples/src/main/scala/org/llm4s/samples/context/

All 8 context management examples demonstrate advanced token window management, compression, and optimization strategies.

ContextPipelineExample

End-to-end context management pipeline with compaction and squeezing.

1
sbt "samples/runMain org.llm4s.samples.context.ContextPipelineExample"

View all context examples →


Embeddings

Location: modules/samples/src/main/scala/org/llm4s/samples/embeddingsupport/

EmbeddingExample

Complete embedding pipeline with similarity search and visualization.

1
sbt "samples/runMain org.llm4s.samples.embeddingsupport.EmbeddingExample"

What it demonstrates:

  • Creating embeddings from text
  • Similarity scoring
  • Vector search
  • Result visualization
  • Chunking and preprocessing

View all embedding examples →


S3LoaderExample

File: S3LoaderExample.scala

Load and ingest documents from AWS S3 buckets with full PDF/DOCX support.

1
2
3
4
5
6
7
# First, set AWS credentials
export AWS_ACCESS_KEY_ID=your-key
export AWS_SECRET_ACCESS_KEY=your-secret
# and an embedding model, as provider/model
export EMBEDDING_MODEL=ollama/nomic-embed-text

sbt "samples/runMain org.llm4s.samples.rag.S3LoaderExample"

What it demonstrates:

  • Loading documents from S3 buckets
  • PDF and DOCX extraction from cloud storage
  • Incremental sync with change detection via ETags
  • Using S3Loader with RAG.sync()
  • LocalStack testing setup

Key Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.llm4s.rag.loader.s3.S3Loader

// Load all supported documents from S3
val loader = S3Loader(
  bucket = "my-documents",
  prefix = "docs/",
  region = "us-east-1"
)

// Sync with change detection
rag.sync(loader) match {
  case Right(stats) =>
    println(s"Added: ${stats.added}, Updated: ${stats.updated}")
  case Left(err) =>
    // The bucket could not be listed: no credentials, missing bucket, access denied.
    // Nothing was deleted from the index.
    println(s"Error: ${err.message}")
}

A listing that fails - on the first page or a later one - is a Left, never a successful sync of 0 documents; an empty bucket is Right with 0. An object that is listed but cannot be read (a transient GetObject error) keeps its indexed version rather than being deleted as gone. With no AWS credentials the example ends with:

1
2
3
4
5
Sync failed: Failed to list S3 objects from s3://my-documents/docs/: Unable to load credentials from any of the providers in the chain ...
Common issues:
  - Check AWS credentials are configured
  ...
Skipping the example query: the index was not synced.

View source →


RAG in a Box

Repository: github.com/llm4s/rag_in_a_box

RAG in a Box is a production-ready RAG server built on the LLM4S framework. It provides a complete solution for document ingestion, semantic search, and AI-powered question answering.

Key Features:

  • REST API for document management and querying
  • Multi-format document support (text, markdown, PDF, URLs)
  • Configurable chunking strategies (simple, sentence, markdown, semantic)
  • Hybrid search with RRF fusion (vector + keyword)
  • Vue.js admin dashboard with document browser and analytics
  • Docker Compose and Kubernetes deployment options
  • JWT authentication, Prometheus metrics, health checks

Current Status:

  • 194 backend tests, 8 frontend E2E specs
  • Security scanning (OWASP, Anchore)
  • Comprehensive documentation

View RAG in a Box →


MCP Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/mcp/

MCPToolExample

Basic MCP tool usage with automatic protocol fallback.

1
sbt "samples/runMain org.llm4s.samples.mcp.MCPToolExample"

What it demonstrates:

  • MCP server connection
  • Tool discovery
  • Protocol handling (stdio, HTTP, SSE)
  • Tool execution

View all MCP examples →


Streaming Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/streaming/

BasicStreamingExample

Fundamental streaming with chunk processing.

1
sbt "samples/runMain org.llm4s.samples.streaming.BasicStreamingExample"

StreamingWithProgressExample

Streaming with real-time progress feedback.

1
sbt "samples/runMain org.llm4s.samples.streaming.StreamingWithProgressExample"

Agent event streams

agent.stream with withStreaming(): text deltas live, model and tool events as they commit. See the streaming guide.

Example Shows
StreamingAgentExample withStreaming(), printing TextDelta, attempt resets
StreamingWithToolsExample ToolCallStarted/ToolCallResult live, ToolExecuted durable with duration
EventCollectionExample collecting one run’s events, replaying the durable ones
AgentStreamIOExample (samples/catseffect) fs2 stream of AgentStreamItem
AgentStreamZIOExample (samples/zio) ZStream of AgentStreamItem
1
sbt "samples/runMain org.llm4s.samples.streaming.StreamingAgentExample"

Reasoning Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/reasoning/

ReasoningModesExample

File: ReasoningModesExample.scala

Using extended thinking/reasoning modes with OpenAI o1/o3 and Anthropic Claude.

1
sbt "samples/runMain org.llm4s.samples.reasoning.ReasoningModesExample"

View source →


Model Examples

Location: modules/samples/src/main/scala/org/llm4s/samples/model/

ModelMetadataExample

File: ModelMetadataExample.scala

Querying model capabilities, pricing, and context limits.

1
sbt "samples/runMain org.llm4s.samples.model.ModelMetadataExample"

View source →

CostTrackingExample

File: CostTrackingExample.scala

What a call costs, at three levels: per request (Completion.estimatedCost), per agent run (AgentResult.usage after a real Agent.run that calls a tool), and per session (a CostTracker the client reports to). It also shows how to price a model the registry does not know, with a ModelRegistryService built from your own ModelMetadata (an immutable snapshot, so nothing global changes), and how MetricsCollector.compose feeds several collectors from one client. A model with no price shows as unknown, never as a guessed number.

1
sbt "samples/runMain org.llm4s.samples.metrics.CostTrackingExample"

View source →

MultiProviderComparisonExample

File: MultiProviderComparisonExample.scala

The same prompt against several providers, side by side: each one’s answer, the tokens it reported and the latency of the call. Providers are named sections under llm4s.providers in your configuration (openai-main, anthropic-main, gemini-main by default; pass other names as arguments), not provider/model strings. A provider that is not configured, or has no key, is reported with the reason and the others still run.

1
2
sbt "samples/runMain org.llm4s.samples.basic.MultiProviderComparisonExample"
sbt "samples/runMain org.llm4s.samples.basic.MultiProviderComparisonExample openai-main ollama-local"

View source →


Other Examples

Interactive Assistant

Location: modules/samples/src/main/scala/org/llm4s/samples/assistant/

AssistantAgentExample - Interactive terminal assistant with session management.

1
sbt "samples/runMain org.llm4s.samples.assistant.AssistantAgentExample"

Commands: /help, /new, /save, /sessions, /quit


Speech

Location: modules/samples/src/main/scala/org/llm4s/samples/

SpeechSamples - Speech-to-text (Vosk, Whisper) and text-to-speech integration.

1
sbt "samples/runMain org.llm4s.samples.SpeechSamples"

Actions

Location: modules/samples/src/main/scala/org/llm4s/samples/actions/

SummarizationExample - Text summarization workflow.

1
sbt "samples/runMain org.llm4s.samples.actions.SummarizationExample"

Running Examples

Basic Run Command

1
sbt "samples/runMain <fully-qualified-class-name>"

Choosing a Provider

The samples load their provider with Llm4sConfig.defaultProvider(), from modules/samples/src/main/resources/application.conf. Nothing reads LLM_MODEL. Out of the box the default is the ollama-local section, using the model llama3:latest; OLLAMA_MODEL and OLLAMA_BASE_URL override it. Pull the default model once, or name one you already have (ollama list):

1
2
3
ollama pull llama3                 # the samples' default model
# or: export OLLAMA_MODEL=llama3.2 # a model already installed
sbt "samples/runMain org.llm4s.samples.basic.BasicLLMCallingExample"

To use a cloud provider, add a section to modules/samples/src/main/resources/application.local.conf (git-ignored, and included by the samples’ application.conf):

1
2
3
4
5
6
llm4s.providers {
  openai-main {
    provider = "openai"
    model    = "gpt-4o"
  }
}

and select it with LLM4S_PROVIDER, which the samples’ application.conf binds to llm4s.providers.provider. The section needs no apiKey: llm4s-openai binds OPENAI_API_KEY to OpenAI’s shared key.

1
2
3
export OPENAI_API_KEY=sk-...
export LLM4S_PROVIDER=openai-main
sbt "samples/runMain org.llm4s.samples.basic.BasicLLMCallingExample"

Only the section being loaded is validated, so a section whose key you have not set does no harm until you select it. See Configuration for more.

The chat-tui sample is the exception: ChatTuiConfig reads LLM_MODEL=<provider>/<model> and the matching API-key variable itself, and falls back to Llm4sConfig.defaultProvider() when LLM_MODEL is unset.

Browse Source

All examples are in the samples directory on GitHub.


Learning Paths

Beginner Path

  1. BasicLLMCallingExample
  2. StreamingExample
  3. WeatherToolExample
  4. SingleStepAgentExample

Intermediate Path

  1. MultiTurnConversationExample
  2. MultiToolExample
  3. LongConversationExample
  4. ConversationPersistenceExample

Advanced Path

  1. ContextPipelineExample
  2. EmbeddingExample
  3. MCPToolExample
  4. Interactive Assistant

Next Steps


Found a useful example pattern? Share it in our Discord!


Table of contents