cllient 0.2.1

A comprehensive Rust client for LLM APIs with unified interface and model management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# API Reference

Complete reference for all three API levels provided by cllient.

## Overview

cllient provides three distinct API layers for different use cases:

1. **[CLI API]#cli-api** - Command-line interface for direct usage
2. **[Runtime API]#runtime-api** - High-level Rust API for applications
3. **[Low-Level Client API]#low-level-client-api** - Direct HTTP client for advanced usage

---

## CLI API

> **Source**: [`src/bin/cllient.rs`]../src/bin/cllient.rs

The command-line interface provides direct access to all cllient functionality:

```bash
# List all available models
cllient list

# Stream responses with real-time output
cllient stream <model-id> "<prompt>"

# Single completion request
cllient ask <model-id> "<prompt>"

# Interactive chat session
cllient chat <model-id>

# Compare responses across models
cllient compare <model1,model2> "<prompt>"
```

**Complete CLI documentation**: [2. CLI Usage Guide](2_cli-usage.md)

---

## Runtime API

> **Source**: [`src/runtime.rs`]../src/runtime.rs | **Main Struct**: [`ModelRegistry`]../src/runtime.rs#L12

High-level API for embedding cllient in Rust applications. Provides a fluent interface for model selection and request building.

### Basic Usage

```rust
use cllient::{ModelRegistry, ClientError};

#[tokio::main]
async fn main() -> Result<(), ClientError> {
    // Initialize with embedded configurations
    let registry = ModelRegistry::new()?;

    // Use specific model
    let response = registry
        .from_id("gpt-4o-mini")?
        .prompt("Hello, world!")
        .send()
        .await?;

    println!("Response: {}", response.content);
    Ok(())
}
```

### Registry Constructors

```rust
// Standard constructor (validates cross-references)
let registry = ModelRegistry::new()?;

// Permissive constructor (allows broken references for debugging)
let registry = ModelRegistry::new_permissive()?;

// With custom config provider
let registry = ModelRegistry::with_provider(my_provider)?;

// With custom provider, permissive
let registry = ModelRegistry::with_provider_permissive(my_provider);
```

### Model Selection Methods

> **Implementation**: [`ModelRegistry methods`]../src/runtime.rs#L22

#### `from_id(model_id)` - Use Specific Model

```rust
let response = registry
    .from_id("claude-3-haiku-20240307")?
    .prompt("Explain quantum computing")
    .send()
    .await?;
```

#### `use_cheapest(pattern)` - Select Cheapest Matching Model

```rust
// Find cheapest Claude model
let response = registry
    .use_cheapest("claude-*")?
    .prompt("Write a haiku")
    .send()
    .await?;

// Find cheapest GPT model
let response = registry
    .use_cheapest("gpt-*")?
    .prompt("Explain AI")
    .send()
    .await?;
```

#### `use_fastest(pattern)` - Select Fastest Matching Model

```rust
let response = registry
    .use_fastest("deepseek-*")?
    .prompt("Quick calculation: 2+2")
    .send()
    .await?;
```

#### `use_best_quality(pattern)` - Select Highest Quality Matching Model

```rust
let response = registry
    .use_best_quality("claude-*")?
    .prompt("Write a detailed analysis")
    .send()
    .await?;
```

### Request Building

> **Source**: [`RequestBuilder`]../src/types.rs

The Runtime API returns a `RequestBuilder` that allows method chaining:

```rust
let response = registry
    .from_id("gpt-4o-mini")?
    .system("You are a helpful assistant")
    .prompt("Explain machine learning")
    .temperature(0.7)
    .max_tokens(1000)
    .top_p(0.9)
    .send()
    .await?;
```

#### RequestBuilder Methods

| Method | Description |
|--------|-------------|
| `system(prompt)` | Set system prompt |
| `prompt(text)` | Set user message (convenience for single turn) |
| `temperature(f64)` | Set temperature parameter |
| `max_tokens(u32)` | Set max output tokens |
| `top_p(f64)` | Set top_p parameter |
| `parameter(key, value)` | Set any custom parameter |
| `append_message(Message)` | Add a message to the conversation |
| `messages(Vec<Message>)` | Set all messages at once |
| `send()` | Send request (requires messages set) |
| `send_text(text)` | Set message and send in one call |
| `send_multimodal(blocks)` | Send multimodal content |
| `stream()` | Start streaming (requires messages set) |
| `stream_text(text)` | Set message and stream in one call |

#### Multi-turn Conversations

```rust
let response = registry
    .from_id("gpt-4o-mini")?
    .append_message(Message::user("What is 2+2?"))
    .append_message(Message::assistant("4"))
    .append_message(Message::user("And what's 4+4?"))
    .send()
    .await?;
```

### Streaming Responses

```rust
let mut stream = registry
    .from_id("deepseek-chat")?
    .prompt("Count to 10")
    .stream()
    .await?;

while let Some(chunk) = stream.next().await {
    match chunk? {
        StreamChunk::Content(text) => print!("{}", text),
        StreamChunk::Done => break,
        StreamChunk::Error(err) => eprintln!("Error: {}", err),
    }
}
```

### Model Information and Discovery

#### `list_models()` - Get All Available Models

```rust
let models = registry.list_models();
println!("Available models: {:#?}", models);
```

#### `list_models_matching(pattern)` - Filter Models by Pattern

```rust
// Get all Claude models
let claude_models = registry.list_models_matching("claude-*")?;

// Get all reasoning models
let reasoning_models = registry.list_models_matching("o[0-9]")?;
```

#### `get_model_info(model_id)` - Get Model Details

```rust
let model_info = registry.get_model_info("gpt-4o-mini")?;
println!("Context window: {}", model_info.capabilities.context_window);
println!("Input cost: ${} per 1K tokens", model_info.pricing.input_per_1k_tokens);
```

#### `list_families()` / `list_models_in_family(family)` - Family Navigation

```rust
// List all model families
let families = registry.list_families();

// List models in a specific family
let gpt_models = registry.list_models_in_family("gpt");
```

#### `list_services()` / `get_service(name)` - Service Access

```rust
// List all available services
let services = registry.list_services();

// Get specific service configuration
let anthropic = registry.get_service("anthropic")?;
```

#### Verification Status Methods

```rust
// List only verified models
let verified = registry.list_verified_models();

// Check if a model is verified
if registry.is_verified("gpt-4o-mini") {
    println!("Model is verified");
}

// List models by status
use cllient::VerificationStatus;
let broken = registry.list_models_by_status(VerificationStatus::Broken);
```

### Query API

> **Source**: [`src/query.rs`]../src/query.rs

The fluent query API provides powerful filtering capabilities:

```rust
let models = registry.query()
    .service("openai")
    .verified()
    .with_vision()
    .context_min(100_000)
    .max_input_price(0.01)
    .order_by_price_asc()
    .limit(10)
    .list();
```

#### Query Filter Methods

| Method | Description |
|--------|-------------|
| `service(name)` | Filter by exact service name |
| `service_like(pattern)` | Filter by service pattern (glob-style) |
| `family(name)` | Filter by exact family name |
| `family_like(pattern)` | Filter by family pattern |
| `verified()` | Only verified models |
| `unverified()` | Only unverified models |
| `status(VerificationStatus)` | Filter by status |
| `with_vision()` | Models with vision capability |
| `with_streaming()` | Models with streaming capability |
| `with_functions()` | Models with function calling |
| `with_json_mode()` | Models with JSON mode |
| `with_system_prompt()` | Models with system prompt support |
| `with_multimodal()` | Models with multimodal capability |
| `max_price(input, output)` | Maximum price per 1k tokens |
| `max_input_price(price)` | Maximum input price |
| `max_output_price(price)` | Maximum output price |
| `context_min(tokens)` | Minimum context window |
| `context_max(tokens)` | Maximum context window |
| `fuzzy(query)` | Fuzzy search on model ID/name |
| `model_id(id)` | Exact model ID match |
| `model_id_like(pattern)` | Model ID pattern match |

#### Query Terminal Methods

| Method | Description |
|--------|-------------|
| `list()` | Return matching model IDs |
| `first()` | Return first matching model |
| `count()` | Return count of matches |
| `cheapest()` | Return cheapest matching model |
| `configs()` | Return full model configs |

#### Query Ordering

| Method | Description |
|--------|-------------|
| `order_by_price_asc()` | Cheapest first |
| `order_by_price_desc()` | Most expensive first |
| `order_by_context_desc()` | Largest context first |
| `limit(n)` | Limit results |

### Error Handling

> **Source**: [`ClientError`]../src/error.rs

```rust
use cllient::{ModelRegistry, ClientError, ConfigError};

match registry.from_id("invalid-model") {
    Ok(builder) => { /* ... */ },
    Err(ClientError::Config(ConfigError::ModelNotFound(model))) => {
        eprintln!("Model '{}' not found", model);
    },
    Err(ClientError::Auth) => {
        eprintln!("Authentication failed");
    },
    Err(ClientError::RateLimit) => {
        eprintln!("Rate limit exceeded");
    },
    Err(e) => eprintln!("Error: {}", e),
}
```

#### ClientError Variants

| Variant | Description |
|---------|-------------|
| `Config(ConfigError)` | Configuration errors (model/service not found, etc.) |
| `Http(reqwest::Error)` | HTTP request errors |
| `YamlSerialization` | YAML parsing errors |
| `JsonSerialization` | JSON parsing errors |
| `Template` / `Render` | Template processing errors |
| `JsonPath(String)` | JSONPath extraction errors |
| `Io(std::io::Error)` | IO errors |
| `Stream(String)` | Streaming errors |
| `RateLimit` | Rate limit exceeded |
| `Auth` | Authentication failed |
| `InvalidModel(String)` | Invalid model specification |
| `InvalidService(String)` | Invalid service specification |
| `ValidationError(String)` | Validation errors |

#### ConfigError Variants

| Variant | Description |
|---------|-------------|
| `ServiceNotFound(String)` | Service not in registry |
| `ModelNotFound(String)` | Model not in registry |
| `InvalidPath(String)` | Invalid configuration path |
| `MissingField(String)` | Required field missing |
| `InvalidYaml(String)` | YAML parsing failed |
| `EnvVarNotFound(String)` | Environment variable missing |
| `BrokenReferences(Vec<String>)` | Models reference non-existent services |

---

## Low-Level Client API

> **Source**: [`src/client.rs`]../src/client.rs

For advanced users needing direct control over HTTP requests and responses.

### ConfigProvider Trait

```rust
/// Trait for configuration providers (file-based or embedded)
pub trait ConfigProvider {
    fn get_service(&self, name: &str) -> Result<&ServiceConfig>;
    fn get_model(&self, id: &str) -> Result<&ModelConfig>;
    fn list_services(&self) -> Vec<&str>;
    fn list_models(&self) -> Vec<&str>;
    fn get_model_with_service(&self, model_id: &str) -> Result<(&ModelConfig, &ServiceConfig)>;
}
```

### LowLevelClient Trait

```rust
#[async_trait]
pub trait LowLevelClient: Send + Sync {
    async fn complete(&self, request: &CompletionRequest) -> Result<CompletionResponse>;
    async fn complete_stream(&self, request: &CompletionRequest) -> Result<Stream>;
}
```

### HttpClient

```rust
use cllient::{HttpClient, CompletionRequest, ContentBlock};

// Create client from model ID (uses embedded configs)
let client = HttpClient::from_model_id(&config_provider, "gpt-4o-mini")?;

// Or create with explicit configs
let client = HttpClient::new(model_config, service_config)?;

// Build request
let request = CompletionRequest::text("user", "Hello, world!")
    .with_system_prompt("You are helpful".to_string())
    .with_parameter("temperature", 0.7)
    .with_parameter("max_tokens", 100);

// Send request
let response = client.complete(&request).await?;
println!("Response: {}", response.content);
```

### Streaming with Low-Level API

```rust
use futures::StreamExt;

let request = CompletionRequest::text("user", "Count to 10")
    .with_streaming(true);

let mut stream = client.complete_stream(&request).await?;

while let Some(chunk) = stream.next().await {
    match chunk {
        Ok(text) => print!("{}", text),
        Err(e) => eprintln!("Error: {}", e),
    }
}
```

### ClientFactory

```rust
use cllient::{ClientFactory, EmbeddedConfigLoader};

let loader = EmbeddedConfigLoader::new()?;
let factory = ClientFactory::new(loader);

// Create clients for different models
let client1 = factory.create_client("gpt-4o-mini")?;
let client2 = factory.create_client("claude-3-haiku-20240307")?;

// List available resources
let models = factory.list_available_models();
let services = factory.list_available_services();
let families = factory.list_families();
```

---

## Type Definitions

> **Source**: [`src/types.rs`]../src/types.rs

### Message Types

```rust
/// Builder for constructing messages with specific roles and content
pub struct Message {
    role: String,
    content: Vec<ContentBlock>,
}

impl Message {
    // Constructors
    pub fn user(text: &str) -> Self;
    pub fn assistant(text: &str) -> Self;
    pub fn system(text: &str) -> Self;
    pub fn custom(role: &str) -> Self;
    pub fn user_multimodal(content: Vec<ContentBlock>) -> Self;

    // Builder methods
    pub fn add_text(self, text: &str) -> Self;
    pub fn add_image(self, data: Vec<u8>, format: ImageFormat) -> Self;
    pub fn add_image_url(self, url: &str) -> Self;
    pub fn add_audio(self, data: Vec<u8>, format: AudioFormat, filename: Option<String>) -> Self;
    pub fn add_document(self, data: Vec<u8>, format: DocumentFormat, filename: Option<String>) -> Self;
    pub fn add_content(self, block: ContentBlock) -> Self;
}

/// Low-level message content (internal)
pub enum MessageContent {
    Text { role: String, content: String },
    Multimodal { role: String, content: Vec<ContentBlock> },
}
```

### Content Types

```rust
pub enum ContentBlock {
    /// Text content
    Text(String),
    /// Binary content (images, audio, documents)
    Binary {
        data: Vec<u8>,
        mime_type: String,
        filename: Option<String>,
    },
    /// URL reference
    Url {
        url: String,
        mime_type: Option<String>,
    },
}

impl ContentBlock {
    pub fn text(content: &str) -> Self;
    pub fn image(data: Vec<u8>, format: ImageFormat) -> Self;
    pub fn image_url(url: &str) -> Self;
    pub fn audio(data: Vec<u8>, format: AudioFormat, filename: Option<String>) -> Self;
    pub fn document(data: Vec<u8>, format: DocumentFormat, filename: Option<String>) -> Self;
    pub fn binary(data: Vec<u8>, mime_type: &str, filename: Option<String>) -> Self;
    pub fn url(url: &str, mime_type: Option<&str>) -> Self;
}

// Format enums
pub enum ImageFormat { Jpeg, Png, Gif, Webp }
pub enum AudioFormat { Wav, Mp3, M4a, Flac, Ogg }
pub enum DocumentFormat { Pdf, Txt, Csv, Doc, Docx }
```

### Request Types

```rust
pub struct CompletionRequest {
    pub messages: Vec<MessageContent>,
    pub system_prompt: Option<String>,
    pub parameters: HashMap<String, Value>,
    pub stream: bool,
}

impl CompletionRequest {
    pub fn new(messages: Vec<MessageContent>) -> Self;
    pub fn text(role: &str, content: &str) -> Self;
    pub fn multimodal(role: &str, content_blocks: Vec<ContentBlock>) -> Self;
    pub fn with_system_prompt(self, prompt: String) -> Self;
    pub fn with_parameter<T: Into<Value>>(self, key: &str, value: T) -> Self;
    pub fn with_streaming(self, stream: bool) -> Self;
}
```

### Response Types

```rust
pub struct CompletionResponse {
    pub content: String,
    pub role: Option<String>,
    pub finish_reason: Option<String>,
    pub usage: Option<Usage>,
    pub raw_response: serde_json::Value,
}

pub struct Usage {
    pub input_tokens: u32,
    pub output_tokens: u32,
    pub total_tokens: Option<u32>,
}

impl Usage {
    pub fn calculate_cost(&self, input_cost_per_1k: f64, output_cost_per_1k: f64) -> f64;
}
```

### Configuration Types

> **Source**: [`src/config.rs`]../src/config.rs

```rust
pub struct ModelConfig {
    pub model: ModelInfo,
    pub capabilities: Capabilities,
    pub pricing: Pricing,
    pub constraints: Constraints,
    pub defaults: HashMap<String, serde_yaml::Value>,
    pub quality: HashMap<String, u8>,
    pub behaviors: HashMap<String, bool>,
    pub use_cases: Vec<String>,
}

pub struct Capabilities {
    pub context_window: u32,
    pub max_output_tokens: u32,
    pub vision: bool,
    pub functions: bool,
    pub streaming: bool,
    pub json_mode: bool,
    pub system_prompt: bool,
    pub multimodal: bool,
}

pub struct Pricing {
    pub currency: Currency,
    pub input_per_1k_tokens: f64,
    pub output_per_1k_tokens: f64,
    pub cached_input_per_1k_tokens: Option<f64>,
}

pub enum VerificationStatus {
    Verified,
    Unverified,
    Broken,
    Deprecated,
    Error,
    NotFound,
}
```

---

## Multimodal Content Support

All APIs support multimodal content through the unified content system:

### Using Message Builder (Recommended)

```rust
use cllient::{Message, ImageFormat, DocumentFormat};

// Simple text message
let msg = Message::user("Hello, world!");

// Image with text
let image_data = std::fs::read("photo.jpg")?;
let msg = Message::user("What's in this image?")
    .add_image(image_data, ImageFormat::Jpeg);

// Image URL
let msg = Message::user("Describe this image")
    .add_image_url("https://example.com/image.jpg");

// Document with question
let pdf_data = std::fs::read("document.pdf")?;
let msg = Message::user("Summarize this document")
    .add_document(pdf_data, DocumentFormat::Pdf, Some("report.pdf".to_string()));

// Multiple content blocks
let msg = Message::user_multimodal(vec![
    ContentBlock::text("Compare these images:"),
    ContentBlock::image(image1_data, ImageFormat::Png),
    ContentBlock::image(image2_data, ImageFormat::Png),
]);
```

### Using ContentBlock Directly

```rust
use cllient::ContentBlock;

// Text content
let text = ContentBlock::text("Hello, world!");

// Binary image
let image = ContentBlock::image(image_bytes, ImageFormat::Jpeg);

// Image URL
let image_url = ContentBlock::image_url("https://example.com/image.jpg");

// Audio content
let audio = ContentBlock::audio(audio_bytes, AudioFormat::Mp3, Some("recording.mp3".to_string()));

// Document
let doc = ContentBlock::document(pdf_bytes, DocumentFormat::Pdf, Some("file.pdf".to_string()));

// Generic binary with custom MIME type
let binary = ContentBlock::binary(data, "application/custom", None);

// Generic URL
let url = ContentBlock::url("https://example.com/file", Some("application/octet-stream"));
```

### Loading from Files

```rust
use cllient::{ContentBlock, FromFile};

// Automatically detects format from extension
let content = ContentBlock::from_file("photo.jpg")?;
let content = ContentBlock::from_file("document.pdf")?;
let content = ContentBlock::from_file("audio.mp3")?;
```

---

## Streaming JSON Output API

> **Source**: [`src/streaming_json.rs`]../src/streaming_json.rs | **Examples**: [`examples/test_streaming_json.rs`]../examples/test_streaming_json.rs

Low-level API for outputting JSON structures incrementally to stdout. Useful for building streaming responses in custom applications.

### StreamingJsonObject

A state machine that outputs JSON object structures as they're being built:

```rust
use cllient::streaming_json::StreamingJsonObject;

let mut json = StreamingJsonObject::new()?;

// Write static fields
json.field_string("model", "gpt-4o-mini")?;
json.field_bool("streaming", true)?;

// Close the object
json.close()?;

// Output (immediate):
// {
//   "model": "gpt-4o-mini",
//   "streaming": true
// }
```

### Streaming String Fields

For dynamic content that arrives incrementally (like LLM responses):

```rust
let mut json = StreamingJsonObject::new()?;
json.field_string("model", "deepseek-chat")?;

// Start streaming field
let mut response = json.field_streaming_string("response")?;

// Write chunks as they arrive
response.write_chunk("Hello")?;
response.write_chunk(" ")?;
response.write_chunk("world")?;

// Close the field
response.close()?;

json.field_bool("success", true)?;
json.close()?;

// Output (streams incrementally):
// {
//   "model": "deepseek-chat",
//   "response": "Hello world",
//   "success": true
// }
```

### Full Example with LLM Streaming

```rust
use cllient::streaming_json::StreamingJsonObject;
use futures::StreamExt;

async fn stream_response(model_id: &str, prompt: &str) -> std::io::Result<()> {
    let mut json = StreamingJsonObject::new()?;

    json.field_string("model", model_id)?;
    json.field_string("prompt", prompt)?;

    // Get LLM stream
    let client = create_client(model_id)?;
    let mut stream = client.complete_stream(&request).await?;

    // Start streaming response field
    let mut response_writer = json.field_streaming_string("response")?;

    // Stream each chunk to JSON output
    while let Some(chunk) = stream.next().await {
        response_writer.write_chunk(&chunk?)?;
    }

    response_writer.close()?;
    json.field_bool("success", true)?;
    json.close()?;

    Ok(())
}
```

### JSON Escaping

The API automatically handles JSON special characters:

```rust
response.write_chunk("Line 1\n")?;           // → "Line 1\n"
response.write_chunk("Quote: \"hello\"")?;  // → "Quote: \"hello\""
response.write_chunk("Path: C:\\file")?;    // → "Path: C:\\file"
```

### API Methods

#### `StreamingJsonObject`

| Method | Description |
|--------|-------------|
| `new()` | Initialize and output opening `{` |
| `field_string(key, value)` | Write a string field |
| `field_bool(key, value)` | Write a boolean field |
| `field_streaming_string(key)` | Start a streaming string field |
| `close()` | Output closing `}` |

#### `StreamingJsonString`

| Method | Description |
|--------|-------------|
| `write_chunk(chunk)` | Append text to the field value |
| `close()` | Output closing quote |

### Tracing Support

All operations are instrumented with `tracing`:

```bash
# Enable debug logging
RUST_LOG=cllient::streaming_json=debug cargo run
RUST_LOG=trace cargo run  # For chunk-level details
```

---

## Provider Integration

> **Service Configs**: [`config/service/`]../config/service/

### Supported Providers

| Provider | Endpoint | Authentication | Streaming |
|----------|----------|----------------|-----------|
| **OpenAI** | `https://api.openai.com/v1/chat/completions` | Bearer Token | ✅ SSE |
| **Anthropic** | `https://api.anthropic.com/v1/messages` | API Key | ✅ SSE |
| **DeepSeek** | `https://api.deepseek.com/chat/completions` | Bearer Token | ✅ SSE |
| **Google** | Gemini API endpoints | API Key | ✅ SSE |
| **Azure OpenAI** | Azure endpoints | API Key | ✅ SSE |

### Adding New Providers

See [4. Configuration Guide](4_configuration.md#adding-providers) for details on adding new LLM providers.

---

## Testing

> **Test Sources**: [`tests/integration/`]../tests/integration/

### API Testing

```bash
# Test CLI API
cargo test --test cli_api_tests

# Test Runtime API  
cargo test --test runtime_api_tests

# Test Low-level Client API
cargo test --test low_level_client_tests

# Test all APIs
cargo test --tests
```

### Example Test Usage

```rust
#[tokio::test]
async fn test_runtime_api() {
    let registry = ModelRegistry::new().unwrap();
    
    let response = registry
        .from_id("gpt-4o-mini").unwrap()
        .prompt("Hello, world!")
        .send()
        .await
        .unwrap();
    
    assert!(!response.content.is_empty());
}
```

---

**Next**: [4. Configuration](4_configuration.md) | [Examples](examples/)