basic-query

command
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 7 Imported by: 0

README

Example: Basic Query

What This Demonstrates

This example shows the simplest way to interact with Claude using the agent-sdk-go: a one-shot query that sends a prompt and receives a complete response.

The v2.Prompt() function is equivalent to unstable_v2_prompt() in the TypeScript SDK.

Prerequisites

  • Claude Code CLI installed and authenticated
  • Go 1.21+

Quick Start

cd examples/basic-query
go run main.go

Or with a custom prompt:

go run main.go "What is 2 + 2?"

Expected Output

=== Basic Query Example ===
Prompt: What is the capital of France? Answer in one word.

Response:
----------------------------------------
Paris
----------------------------------------

Session ID: prompt-1737175678901234567
Duration: 1.234s

Key Patterns

Pattern 1: One-Shot Prompt

The simplest way to query Claude - send a prompt, get a response:

result, err := v2.Prompt(ctx, "Your question here",
    v2.WithPromptModel("claude-sonnet-4-20250514"),
    v2.WithPromptTimeout(60*time.Second),
)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.Result)
Pattern 2: CLI Availability Check

Always check that the Claude CLI is available before making queries:

if !cli.IsCLIAvailable() {
    log.Fatal("Claude CLI not found")
}
Pattern 3: Context with Timeout

Use Go's context for cancellation and timeouts:

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

TypeScript Equivalent

This ports the TypeScript example from: https://github.com/anthropics/claude-agent-sdk-demos/tree/main/hello-world-v2

TypeScript:

const result = await unstable_v2_prompt('What is the capital of France?', { model: 'sonnet' });
if (result.subtype === 'success') {
    console.log(`Answer: ${result.result}`);
}

Go:

result, err := v2.Prompt(ctx, "What is the capital of France?",
    v2.WithPromptModel("claude-sonnet-4-20250514"),
)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.Result)

Documentation

Overview

Package main demonstrates a basic one-shot query using the Claude Agent SDK.

This example shows: - Using v2.Prompt() for one-shot queries - Handling the result with timing information - Proper error handling

Based on TypeScript example: https://github.com/anthropics/claude-agent-sdk-demos/tree/main/hello-world-v2

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL