Skip to content

Prompt & Streaming

Translation in Progress

This page is being translated. Content below is a placeholder.

System Prompt

System Prompt defines the Agent's behavior, personality, and constraints.

Basic Usage

typescript
const model = genAI.getGenerativeModel({
  model: 'gemini-2.5-flash',
  systemInstruction: `You are a coding assistant.
You can read and write files.
Always explain what you're doing before taking action.`
})

gemini-cli's System Prompt Structure

typescript
const systemPrompt = `
# Role
You are a coding assistant...

# Tools Available
- read_file: Read file contents
- write_file: Write to files
- run_command: Execute shell commands

# Guidelines
1. Understand the task first
2. Read relevant files before editing
3. Explain actions to user
...
`

Streaming Output

Streaming lets users see responses in real-time.

Basic Streaming

typescript
const result = await chat.sendMessageStream('Hello')

for await (const chunk of result.stream) {
  const text = chunk.text()
  if (text) {
    process.stdout.write(text)
  }
}

Handling Tool Calls in Streams

typescript
const result = await chat.sendMessageStream(message)
let functionCalls: any[] = []

for await (const chunk of result.stream) {
  // Output text in real-time
  const text = chunk.text()
  if (text) {
    process.stdout.write(text)
  }

  // Collect tool calls
  const calls = chunk.functionCalls()
  if (calls) {
    functionCalls.push(...calls)
  }
}

// Process tool calls after stream ends
if (functionCalls.length) {
  // Execute tools...
}

Summary

  • System Prompt defines Agent behavior
  • Streaming provides real-time feedback
  • Tool calls can be collected during streaming

Next

Dive into the tool system: Function Calling →

Learn AI Agent development through real source code