Files
kit/advanced/json-output/index.html
T

107 lines
3.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Output | Kit</title>
<meta name="description" content="Machine-readable JSON output for scripting and automation.">
<link rel="canonical" href="/advanced/json-output">
<link rel="stylesheet" href="/assets/index-Di_r5hA0.css">
<script type="module" src="/assets/index-BbQ_p9l4.js"></script>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"TechArticle","headline":"JSON Output","description":"Machine-readable JSON output for scripting and automation.","url":"https://go-kit.dev/advanced/json-output","isPartOf":{"@type":"WebSite","name":"Kit","url":"https://go-kit.dev"}}</script>
</head>
<body>
<div id="tome-root"></div>
<div data-pagefind-body style="display:none"><h1>JSON Output</h1>
# JSON Output
Use the `--json` flag to get structured output for scripting and automation:
```bash
kit "Explain main.go" --json --quiet --no-session
```
## Response format
```json
{
"response": "Final assistant response text",
"model": "anthropic/claude-haiku-latest",
"stop_reason": "end_turn",
"session_id": "a1b2c3d4e5f6",
"usage": {
"input_tokens": 1024,
"output_tokens": 512,
"total_tokens": 1536,
"cache_read_tokens": 0,
"cache_creation_tokens": 0
},
"messages": [
{
"role": "assistant",
"parts": [
{"type": "text", "data": "..."},
{"type": "tool_call", "data": {"name": "...", "args": "..."}},
{"type": "tool_result", "data": {"name": "...", "result": "..."}}
]
}
]
}
```
## Fields
### Top-level
| Field | Type | Description |
|-------|------|-------------|
| `response` | string | The final assistant response text |
| `model` | string | The model that was used |
| `stop_reason` | string | Why the model stopped (e.g., `end_turn`) |
| `session_id` | string | Session identifier (omitted in `--no-session` mode) |
| `usage` | object | Token usage statistics |
| `messages` | array | Full conversation history |
### Usage
| Field | Type | Description |
|-------|------|-------------|
| `input_tokens` | int | Tokens sent to the model |
| `output_tokens` | int | Tokens generated by the model |
| `total_tokens` | int | Sum of input and output tokens |
| `cache_read_tokens` | int | Tokens read from prompt cache |
| `cache_creation_tokens` | int | Tokens written to prompt cache |
### Message parts
Each message contains a `parts` array with typed entries:
| Type | Description |
|------|-------------|
| `text` | Assistant text content |
| `tool_call` | Tool invocation with name and args |
| `tool_result` | Tool execution result |
| `reasoning` | Extended thinking content |
| `finish` | End-of-turn marker |
## Parsing in scripts
### bash + jq
```bash
result=$(kit "Count files" --json --quiet --no-session)
response=$(echo "$result" | jq -r '.response')
tokens=$(echo "$result" | jq '.usage.total_tokens')
```
### Go SDK
For Go programs, use the SDK's `PromptResult` method instead of parsing JSON:
```go
result, err := host.PromptResult(ctx, "Count files")
fmt.Println(result.Response)
fmt.Println(result.Usage.TotalTokens)
```</div>
</body>
</html>