paladin-ai 0.4.3

Enterprise AI orchestration framework with multi-agent coordination patterns
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# Conclave Pattern Guide

Multi-expert synthesis orchestration implementing the Mixture-of-Agents approach. Multiple specialized Paladins analyze a task in parallel, then an aggregator synthesizes their diverse perspectives into a comprehensive response.

## Table of Contents

- [Overview]#overview
- [Quick Start]#quick-start
- [Configuration]#configuration
- [Programmatic API]#programmatic-api
- [YAML Configuration]#yaml-configuration
- [CLI Usage]#cli-usage
- [Use Cases]#use-cases
- [Error Handling]#error-handling
- [Observability]#observability
- [Best Practices]#best-practices
- [Troubleshooting]#troubleshooting

## Overview

The Conclave pattern solves problems requiring **multiple expert perspectives** that must be **intelligently synthesized**. Unlike simple parallel execution (Phalanx), Conclave specifically focuses on combining diverse viewpoints through an aggregator agent.

### When to Use Conclave

**✅ Use Conclave When:**
- Decisions benefit from multiple perspectives (technical, business, security, etc.)
- You need diverse expert opinions synthesized into actionable recommendations
- Different stakeholders have unique concerns that must all be addressed
- Quality improves through deliberate multi-perspective analysis

**❌ Don't Use Conclave When:**
- Single perspective is sufficient
- All agents would provide identical analysis
- Simple parallel processing without synthesis is adequate (use Phalanx instead)
- Real-time response is critical (Conclave adds synthesis overhead)

### Architecture

```text
                    ┌──────────────┐
                    │   Input      │
                    │   Query      │
                    └──────┬───────┘
         ┌─────────────────┼─────────────────┐
         │                 │                 │
         ▼                 ▼                 ▼
  ┌─────────────┐   ┌─────────────┐   ┌─────────────┐
  │  Expert 1   │   │  Expert 2   │   │  Expert 3   │
  │ (Technical) │   │ (Business)  │   │ (Security)  │
  └──────┬──────┘   └──────┬──────┘   └──────┬──────┘
         │                 │                 │
         └─────────────────┼─────────────────┘
                    ┌─────────────┐
                    │ Aggregator  │
                    │  Synthesis  │
                    └──────┬──────┘
                    ┌─────────────┐
                    │   Final     │
                    │  Response   │
                    └─────────────┘
```

### Key Benefits

1. **Higher Quality Outputs**: Multiple perspectives catch blind spots
2. **Comprehensive Analysis**: Technical, business, security, etc. all considered
3. **Balanced Decisions**: Aggregator weighs competing priorities
4. **Resilience**: Continues even if some experts fail
5. **Traceable Reasoning**: See each expert's input to final decision

## Quick Start

### Minimal Example

```rust
use paladin::prelude::*;
use paladin::battalion::conclave::*;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let llm_adapter = Arc::new(OpenAiAdapter::new().build()?);

    // Create 3 experts with different perspectives
    let technical = create_paladin(llm_adapter.clone(),
        "TechnicalExpert",
        "You are a technical architect. Analyze from a technical perspective."
    )?;

    let business = create_paladin(llm_adapter.clone(),
        "BusinessExpert",
        "You are a business strategist. Analyze from a business perspective."
    )?;

    let security = create_paladin(llm_adapter.clone(),
        "SecurityExpert",
        "You are a security expert. Analyze from a security perspective."
    )?;

    // Create aggregator to synthesize expert outputs
    let aggregator = create_paladin(llm_adapter.clone(),
        "Aggregator",
        "Synthesize the expert analyses into a comprehensive recommendation."
    )?;

    // Configure Conclave
    let config = ConclaveConfig::new("expert-panel", BattalionConfig::default())
        .with_timeout(300)
        .with_retry_attempts(2);

    // Build Conclave
    let conclave = Conclave::new(
        vec![technical, business, security],
        aggregator,
        config
    )?;

    // Execute
    let service = ConclaveExecutionService::new(paladin_port);
    let result = service.execute(&conclave,
        "Should we migrate to microservices?"
    ).await?;

    println!("Final Recommendation:\n{}", result.aggregated_output.output);
    Ok(())
}

fn create_paladin(
    llm: Arc<dyn LlmPort>,
    name: &str,
    prompt: &str
) -> Result<Paladin, Box<dyn std::error::Error>> {
    PaladinBuilder::new(llm)
        .name(name)
        .system_prompt(prompt)
        .temperature(0.7)
        .build()
}
```

## Configuration

### ConclaveConfig Options

```rust
pub struct ConclaveConfig {
    /// Conclave name (required)
    name: String,

    /// Battalion base configuration
    battalion_config: BattalionConfig,

    /// Maximum execution time (seconds)
    timeout_seconds: u64,

    /// Retry attempts for failed experts (default: 2)
    max_retry_attempts: u32,

    /// Custom synthesis prompt (optional)
    synthesis_prompt: Option<String>,

    /// Include expert names in aggregator input (default: true)
    include_expert_names: bool,

    /// Max tokens per expert before truncation (optional)
    max_expert_tokens: Option<usize>,

    /// Observability level (default: Standard)
    observability: ObservabilityLevel,
}
```

### Builder Pattern

```rust
let config = ConclaveConfig::new("my-conclave", battalion_config)
    .with_timeout(600)                    // 10 minutes
    .with_retry_attempts(3)               // Retry up to 3 times
    .with_observability(ObservabilityLevel::Verbose)
    .with_expert_names(true)              // Show expert attribution
    .with_max_expert_tokens(2000)         // Truncate long outputs
    .with_synthesis_prompt(               // Override aggregator prompt
        "Focus only on technical feasibility. YES/NO answer required."
    );
```

### Retry Configuration

Conclave uses **exponential backoff with jitter** for retries:

```
Attempt 1: 1 second  ± 20% jitter
Attempt 2: 2 seconds ± 20% jitter
Attempt 3: 4 seconds ± 20% jitter
Attempt 4: 8 seconds ± 20% jitter
Attempt 5: 16 seconds ± 20% jitter
```

Example configuration:

```rust
let config = ConclaveConfig::new("resilient", battalion_config)
    .with_retry_attempts(3)  // Total 4 attempts (1 initial + 3 retries)
    .with_timeout(300);      // Overall timeout for all attempts
```

### Observability Levels

```rust
pub enum ObservabilityLevel {
    Minimal,   // Errors and final result only
    Standard,  // Progress updates + timing (default)
    Verbose,   // Detailed logs, individual outputs, retries
}
```

**Minimal**: Production systems with log aggregation
```rust
.with_observability(ObservabilityLevel::Minimal)
```

**Standard**: Development and staging (recommended)
```rust
.with_observability(ObservabilityLevel::Standard)
```

**Verbose**: Debugging and troubleshooting
```rust
.with_observability(ObservabilityLevel::Verbose)
```

## Programmatic API

### Expert Creation

Create diverse experts with specialized roles:

```rust
// Technical Expert - Focus on implementation details
let technical_expert = PaladinBuilder::new(llm_port.clone())
    .name("TechnicalArchitect")
    .system_prompt(
        "You are a senior technical architect with 15+ years experience \
         in distributed systems. Analyze the proposal focusing on:\n\
         - System architecture and design patterns\n\
         - Scalability and performance\n\
         - Technology stack recommendations\n\
         - Implementation risks and complexity"
    )
    .temperature(0.7)
    .max_loops(3)
    .build()?;

// Business Expert - Focus on ROI and strategy
let business_expert = PaladinBuilder::new(llm_port.clone())
    .name("BusinessStrategist")
    .system_prompt(
        "You are a business strategist and product manager. Analyze focusing on:\n\
         - Market opportunity and competitive positioning\n\
         - Cost-benefit analysis and ROI projections\n\
         - Resource requirements (team, budget, timeline)\n\
         - Stakeholder impact across departments"
    )
    .temperature(0.7)
    .max_loops(3)
    .build()?;

// Security Expert - Focus on risks and compliance
let security_expert = PaladinBuilder::new(llm_port.clone())
    .name("SecurityExpert")
    .system_prompt(
        "You are a security expert specializing in application security. Analyze focusing on:\n\
         - Threat modeling and attack surface\n\
         - Required security controls (auth, encryption, etc.)\n\
         - Compliance requirements (GDPR, SOC 2, HIPAA)\n\
         - Security testing requirements"
    )
    .temperature(0.7)
    .max_loops(3)
    .build()?;
```

### Aggregator Creation

The aggregator synthesizes expert outputs:

```rust
let aggregator = PaladinBuilder::new(llm_port.clone())
    .name("SynthesisAggregator")
    .system_prompt(
        "You are a synthesis expert combining multiple perspectives. \
         You receive technical, business, and security analyses. \
         Your synthesis should:\n\
         1. Create an executive summary with clear recommendation\n\
         2. Identify common themes across experts\n\
         3. Highlight unique insights from each perspective\n\
         4. Resolve contradictions by weighing evidence\n\
         5. Provide prioritized action items\n\
         6. Outline critical success factors and risks\n\n\
         Structure with clear sections. Integrate thoughtfully, don't just concatenate."
    )
    .temperature(0.5)  // Lower temperature for consistent synthesis
    .max_loops(2)
    .build()?;
```

### Building and Executing

```rust
// Create Conclave
let experts = vec![technical_expert, business_expert, security_expert];

let config = ConclaveConfig::new("expert-panel", BattalionConfig::default())
    .with_timeout(300)
    .with_retry_attempts(2)
    .with_observability(ObservabilityLevel::Standard);

let conclave = Conclave::new(experts, aggregator, config)?;

// Execute
let service = ConclaveExecutionService::new(paladin_port);
let result = service.execute(&conclave,
    "Should we implement real-time WebSocket notifications?"
).await?;

// Access results
println!("Status: {:?}", result.status);
println!("Execution time: {}ms", result.execution_time_ms);
println!("Expert success rate: {}/{}",
    result.successful_expert_count(),
    conclave.expert_count()
);

// Individual expert outputs
for (name, output) in result.expert_outputs.iter() {
    println!("\n{}: {}", name, output.output);
}

// Final synthesized output
println!("\nFinal Recommendation:\n{}", result.aggregated_output.output);
```

### Error Handling with Partial Success

```rust
match service.execute(&conclave, input).await {
    Ok(result) => {
        if result.successful_expert_count() < conclave.expert_count() {
            eprintln!("Warning: {} experts failed",
                conclave.expert_count() - result.successful_expert_count());
        }

        // Check aggregation success
        if result.status == ConclaveStatus::Completed {
            println!("Success: {}", result.aggregated_output.output);
        } else {
            eprintln!("Aggregation failed but partial results available");
            for (name, output) in result.expert_outputs.iter() {
                println!("{}: {}", name, output.output);
            }
        }
    }
    Err(ConclaveError::AllExpertsFailed) => {
        eprintln!("Critical: All experts failed");
    }
    Err(e) => {
        eprintln!("Error: {}", e);
    }
}
```

## YAML Configuration

### Basic YAML Structure

Create `conclave.yaml`:

```yaml
type: conclave
name: "expert-panel"

experts:
  - inline:
      name: "TechnicalExpert"
      system_prompt: |
        You are a technical architect...
      model: "gpt-4o"
      temperature: 0.7
      max_loops: 3
      timeout_seconds: 300
      stop_words: []
      provider:
        type: openai

  - inline:
      name: "BusinessExpert"
      system_prompt: |
        You are a business strategist...
      model: "gpt-4o"
      temperature: 0.7
      max_loops: 3
      timeout_seconds: 300
      stop_words: []
      provider:
        type: openai

aggregator:
  inline:
    name: "Aggregator"
    system_prompt: |
      Synthesize expert analyses...
    model: "gpt-4o"
    temperature: 0.5
    max_loops: 2
    timeout_seconds: 300
    stop_words: []
    provider:
      type: openai

timeout_seconds: 300
retry_attempts: 2
include_expert_names: true
observability_level: "standard"
```

### External Paladin References

Reference pre-defined Paladin configs:

```yaml
type: conclave
name: "expert-panel"

experts:
  - file: "configs/technical_expert.yaml"
  - file: "configs/business_expert.yaml"
  - file: "configs/security_expert.yaml"

aggregator:
  file: "configs/synthesis_aggregator.yaml"

timeout_seconds: 300
retry_attempts: 2
```

### Advanced Options

```yaml
type: conclave
name: "custom-conclave"

experts:
  - inline:
      # ... expert configs ...

aggregator:
  inline:
    # ... aggregator config ...

# Custom synthesis prompt (overrides aggregator's system_prompt)
synthesis_prompt: |
  Focus ONLY on technical feasibility.
  Provide YES/NO recommendation with brief justification.
  Ignore business and security concerns for this analysis.

# Include expert names in aggregator input
include_expert_names: true

# Truncate expert outputs to 2000 tokens before aggregation
max_expert_output_tokens: 2000

# Verbose logging for debugging
observability_level: "verbose"

# Aggressive retry policy
timeout_seconds: 600
retry_attempts: 3
```

## CLI Usage

### Generate Template

Create a new Conclave configuration:

```bash
paladin battalion new my-experts --type conclave --output conclave.yaml
```

This generates a template with 3 experts (Technical, Business, Security) and an aggregator with helpful comments.

### Run Conclave

Execute a Conclave configuration:

```bash
paladin battalion run --config conclave.yaml --type conclave
```

You'll be prompted for input:

```
? Enter task for expert analysis: Should we migrate to microservices?
```

### Output to JSON

Save structured output:

```bash
paladin battalion run -c conclave.yaml -t conclave -o result.json
```

### Verbose Mode

See detailed execution logs:

```bash
paladin battalion run -c conclave.yaml -t conclave --verbose
```

Output includes:
- Expert execution progress
- Individual expert outputs (truncated)
- Execution timing
- Success/failure rates
- Final aggregated output

## Use Cases

### 1. Technical Decision Making

**Scenario**: Evaluate architectural changes

**Experts**:
- Technical Architect (implementation feasibility)
- DevOps Engineer (operational impact)
- Security Engineer (security implications)

**Input**: "Should we adopt Kubernetes for our infrastructure?"

**Value**: Comprehensive evaluation covering development, operations, and security perspectives.

### 2. Product Feature Evaluation

**Scenario**: Prioritize product features

**Experts**:
- Product Manager (market fit, user value)
- Engineering Lead (implementation complexity)
- Data Scientist (data requirements, ML feasibility)

**Input**: "Should we build an in-house recommendation engine?"

**Value**: Balanced view of business value vs. technical effort.

### 3. Code Review

**Scenario**: Comprehensive code quality analysis

**Experts**:
- Security Reviewer (vulnerability detection)
- Performance Reviewer (optimization opportunities)
- Maintainability Reviewer (code quality, patterns)

**Input**: Code snippet or PR description

**Value**: Multi-dimensional review catching issues from different angles.

### 4. Compliance Assessment

**Scenario**: Evaluate regulatory compliance

**Experts**:
- GDPR Expert (data protection requirements)
- SOC 2 Expert (security controls)
- Industry Expert (sector-specific regulations)

**Input**: "Assess compliance requirements for storing health data"

**Value**: Comprehensive compliance coverage across multiple frameworks.

### 5. Strategic Planning

**Scenario**: Long-term strategic decisions

**Experts**:
- Market Analyst (competitive landscape, trends)
- Financial Advisor (budget, ROI projections)
- Risk Manager (strategic risks, mitigation)

**Input**: "Should we expand to European markets in 2025?"

**Value**: Well-rounded strategic recommendation considering multiple stakeholder concerns.

## Error Handling

### Partial Success Scenarios

Conclave continues even if some experts fail:

```rust
let result = service.execute(&conclave, input).await?;

// Check success rate
let success_rate = result.successful_expert_count() as f64 /
                  conclave.expert_count() as f64;

if success_rate < 0.5 {
    eprintln!("Warning: Less than 50% experts succeeded");
}

// Aggregation proceeds with available expert outputs
if result.status == ConclaveStatus::PartialSuccess {
    println!("Aggregation completed with partial expert data");
}
```

### Retry Behavior

Failed experts are automatically retried:

```rust
let config = ConclaveConfig::new("resilient", battalion_config)
    .with_retry_attempts(3)  // Retry up to 3 times
    .with_timeout(300);      // Overall timeout includes retries
```

**Retry triggers**:
- Network timeouts
- API rate limits (429 errors)
- Temporary service unavailability (503 errors)

**No retry for**:
- Authentication failures (401, 403)
- Invalid requests (400)
- Not found (404)
- Exceeded overall timeout

### Error Recovery

```rust
match service.execute(&conclave, input).await {
    Ok(result) => {
        match result.status {
            ConclaveStatus::Completed => {
                // All experts succeeded, aggregation successful
                println!("Success: {}", result.aggregated_output.output);
            }
            ConclaveStatus::PartialSuccess => {
                // Some experts failed, but aggregation succeeded
                println!("Partial success: {}", result.aggregated_output.output);
                log::warn!("Failed experts: {}",
                    conclave.expert_count() - result.successful_expert_count());
            }
            ConclaveStatus::Failed => {
                // Aggregation failed
                log::error!("Aggregation failed");
                // Access individual expert outputs if available
                for (name, output) in result.expert_outputs.iter() {
                    println!("{}: {}", name, output.output);
                }
            }
        }
    }
    Err(ConclaveError::AllExpertsFailed) => {
        log::error!("All experts failed - cannot proceed with aggregation");
    }
    Err(ConclaveError::Timeout(secs)) => {
        log::error!("Execution exceeded {} second timeout", secs);
    }
    Err(e) => {
        log::error!("Unexpected error: {}", e);
    }
}
```

## Observability

### Logging Levels

Configure observability to match your environment:

**Minimal** (Production):
```rust
.with_observability(ObservabilityLevel::Minimal)
```

Logs only:
- Critical errors
- Final execution status
- Total execution time

**Standard** (Staging/Development):
```rust
.with_observability(ObservabilityLevel::Standard)
```

Logs:
- Expert execution start/completion
- Retry attempts
- Partial failure warnings
- Aggregation timing
- Success/failure counts

**Verbose** (Debugging):
```rust
.with_observability(ObservabilityLevel::Verbose)
```

Logs:
- All Standard logs PLUS:
- Individual expert outputs (truncated)
- Detailed retry information
- Token counts per expert
- Timing breakdown by phase

### Execution Metrics

Access detailed metrics from results:

```rust
let result = service.execute(&conclave, input).await?;

// Overall metrics
println!("Total time: {}ms", result.execution_time_ms);
println!("Status: {:?}", result.status);

// Expert-level metrics
for (name, expert_result) in result.expert_outputs.iter() {
    println!("{}: {}ms, {} tokens, {} loops",
        name,
        expert_result.execution_time_ms,
        expert_result.token_count,
        expert_result.loop_count
    );
}

// Aggregation metrics
println!("Aggregator: {}ms, {} tokens",
    result.aggregated_output.execution_time_ms,
    result.aggregated_output.token_count
);

// Success rate
println!("Success rate: {}/{}",
    result.successful_expert_count(),
    conclave.expert_count()
);
```

### Structured Logging

Integrate with structured logging frameworks:

```rust
use log::{info, warn, error};

let result = service.execute(&conclave, input).await?;

info!(
    "Conclave execution completed";
    "conclave_name" => &conclave.name(),
    "status" => format!("{:?}", result.status),
    "execution_ms" => result.execution_time_ms,
    "expert_count" => conclave.expert_count(),
    "successful_experts" => result.successful_expert_count(),
);

if result.successful_expert_count() < conclave.expert_count() {
    warn!(
        "Partial expert failure";
        "failed_count" => conclave.expert_count() - result.successful_expert_count(),
    );
}
```

## Best Practices

### Expert Configuration

**1. Recommended Number of Experts: 3-5**

- **Minimum 2**: Required for diversity
- **Optimal 3-4**: Balanced quality vs. cost/latency
- **Maximum 5-6**: Diminishing returns beyond this

**2. Ensure Expert Diversity**

❌ Don't create redundant experts:
```rust
let expert1 = create_expert("Expert1", "You are a technical expert");
let expert2 = create_expert("Expert2", "You are a technical expert");
// Same perspective - wasteful!
```

✅ Create distinct perspectives:
```rust
let technical = create_expert("Technical", "Architecture and implementation");
let business = create_expert("Business", "ROI and strategy");
let security = create_expert("Security", "Risks and compliance");
// Different perspectives - valuable diversity
```

**3. Use Lower Temperature for Aggregator**

Experts can be creative (temperature 0.6-0.8), but aggregator should be consistent:

```rust
// Experts: Creative analysis
let expert = PaladinBuilder::new(llm)
    .temperature(0.7)
    .build()?;

// Aggregator: Consistent synthesis
let aggregator = PaladinBuilder::new(llm)
    .temperature(0.5)  // Lower for consistency
    .build()?;
```

### Prompt Engineering

**1. Structure Expert Prompts**

Use clear sections in system prompts:

```rust
let expert = create_expert(
    "TechnicalExpert",
    "You are a senior technical architect.\n\
     \n\
     Analyze the input focusing on:\n\
     - System architecture and design patterns\n\
     - Scalability and performance considerations\n\
     - Technology stack recommendations\n\
     - Implementation risks and complexity\n\
     \n\
     Provide specific technical details.\n\
     Cite proven patterns and best practices."
);
```

**2. Aggregator Synthesis Instructions**

Be explicit about synthesis requirements:

```rust
let aggregator = create_expert(
    "Aggregator",
    "Synthesize expert analyses following these steps:\n\
     1. Create executive summary with clear recommendation\n\
     2. Identify common themes across all experts\n\
     3. Highlight unique insights from each perspective\n\
     4. Resolve contradictions by weighing evidence\n\
     5. Provide prioritized action items\n\
     6. Outline critical success factors and risks\n\
     \n\
     DO NOT simply concatenate expert outputs.\n\
     Integrate thoughtfully into coherent narrative."
);
```

**3. Use synthesis_prompt for Task-Specific Focus**

Override aggregator behavior for specific tasks:

```rust
let config = ConclaveConfig::new("focused", battalion_config)
    .with_synthesis_prompt(
        "Focus ONLY on technical feasibility. \
         Ignore business and security concerns. \
         Provide YES/NO recommendation with 2-3 sentence justification."
    );
```

### Performance Optimization

**1. Set Appropriate Timeouts**

```rust
// Quick analysis
let config = ConclaveConfig::new("quick", battalion_config)
    .with_timeout(60);  // 1 minute

// Thorough analysis
let config = ConclaveConfig::new("thorough", battalion_config)
    .with_timeout(600);  // 10 minutes
```

**2. Truncate Verbose Expert Outputs**

Prevent token limit issues:

```rust
let config = ConclaveConfig::new("optimized", battalion_config)
    .with_max_expert_tokens(2000);  // Limit per expert
```

**3. Parallel Execution is Automatic**

Experts execute concurrently - no additional configuration needed.

### Cost Management

**1. Choose Appropriate Models**

```rust
// Experts: Use fast, cost-effective models
let expert = PaladinBuilder::new(llm)
    .model("gpt-4o-mini")  // Cheaper model
    .temperature(0.7)
    .build()?;

// Aggregator: Use more capable model for synthesis
let aggregator = PaladinBuilder::new(llm)
    .model("gpt-4o")  // Better model for complex synthesis
    .temperature(0.5)
    .build()?;
```

**2. Limit max_loops**

Prevent excessive LLM calls:

```rust
let expert = PaladinBuilder::new(llm)
    .max_loops(2)  // Reasonable limit
    .build()?;
```

**3. Monitor Token Usage**

```rust
let result = service.execute(&conclave, input).await?;

let total_tokens: usize = result.expert_outputs.values()
    .map(|r| r.token_count)
    .sum::<usize>() + result.aggregated_output.token_count;

println!("Total tokens used: {}", total_tokens);
```

## Troubleshooting

### Problem: All Experts Fail

**Symptoms**:
- Error: `ConclaveError::AllExpertsFailed`
- No expert outputs in result

**Possible Causes**:
1. API key issues
2. Network connectivity problems
3. Rate limiting
4. Invalid model names

**Solutions**:
```rust
// 1. Verify API keys
std::env::var("OPENAI_API_KEY").expect("API key not set");

// 2. Increase timeout
let config = ConclaveConfig::new("patient", battalion_config)
    .with_timeout(600);  // Longer timeout

// 3. Add more retry attempts
let config = ConclaveConfig::new("persistent", battalion_config)
    .with_retry_attempts(5);

// 4. Enable verbose logging
let config = ConclaveConfig::new("debug", battalion_config)
    .with_observability(ObservabilityLevel::Verbose);
```

### Problem: Aggregation Fails Despite Successful Experts

**Symptoms**:
- Expert outputs are present
- `result.status == ConclaveStatus::Failed`
- Aggregation error in logs

**Possible Causes**:
1. Aggregator timeout (processing combined expert outputs)
2. Token limit exceeded (too much expert output)
3. Aggregator model capacity issues

**Solutions**:
```rust
// 1. Increase aggregator-specific timeout
let aggregator = PaladinBuilder::new(llm)
    .timeout_seconds(600)  // Longer timeout for synthesis
    .build()?;

// 2. Truncate expert outputs
let config = ConclaveConfig::new("limited", battalion_config)
    .with_max_expert_tokens(1500);

// 3. Use more capable aggregator model
let aggregator = PaladinBuilder::new(llm)
    .model("gpt-4o")  // Upgrade from mini
    .build()?;
```

### Problem: Poor Quality Synthesis

**Symptoms**:
- Aggregator simply concatenates expert outputs
- Missing integration of perspectives
- No actionable recommendations

**Solutions**:
```rust
// 1. Improve aggregator prompt
let aggregator = create_expert(
    "Aggregator",
    "You are a synthesis expert. Your role is to INTEGRATE (not concatenate) \
     the expert analyses. Create a coherent narrative that:\n\
     - Identifies patterns and common themes\n\
     - Highlights contradictions and resolves them\n\
     - Provides clear, actionable recommendations\n\
     - Structures output with sections and bullet points"
);

// 2. Use synthesis_prompt for task-specific guidance
let config = ConclaveConfig::new("guided", battalion_config)
    .with_synthesis_prompt(
        "Combine expert analyses into a single recommendation. \
         Format as: Executive Summary, Key Findings, Recommendation, Next Steps."
    );

// 3. Lower aggregator temperature for consistency
let aggregator = PaladinBuilder::new(llm)
    .temperature(0.3)  // Very consistent
    .build()?;
```

### Problem: Slow Execution

**Symptoms**:
- Execution takes longer than expected
- Timeout errors

**Possible Causes**:
1. Sequential expert execution (shouldn't happen - experts are parallel)
2. Slow individual experts
3. Excessive retries

**Solutions**:
```rust
// 1. Verify parallel execution (automatic, but check logs)
let config = ConclaveConfig::new("fast", battalion_config)
    .with_observability(ObservabilityLevel::Verbose);

// 2. Reduce expert max_loops
let expert = PaladinBuilder::new(llm)
    .max_loops(1)  // Single pass
    .build()?;

// 3. Limit retry attempts
let config = ConclaveConfig::new("quick", battalion_config)
    .with_retry_attempts(1);  // One retry only

// 4. Use faster models
let expert = PaladinBuilder::new(llm)
    .model("gpt-4o-mini")
    .build()?;
```

### Problem: Inconsistent Expert Names in Output

**Symptoms**:
- Expert outputs lack attribution
- Can't tell which expert said what

**Solution**:
```rust
let config = ConclaveConfig::new("attributed", battalion_config)
    .with_expert_names(true);  // Ensure this is set
```

## See Also

- [Battalion Patterns Guide]./battalion-patterns.md - Other orchestration patterns
- [Paladin Configuration]./paladin-configuration.md - Expert setup
- [Examples]../../examples/conclave_expert_panel.rs - Complete working examples
- [CLI Configs]../../examples/cli_configs/conclave_expert_panel.yaml - YAML templates