narcolepticchicken commited on
Commit
dce68d9
·
verified ·
1 Parent(s): 6537c78

Upload examples/integration_example.py

Browse files
Files changed (1) hide show
  1. examples/integration_example.py +118 -0
examples/integration_example.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Example integration of Agent Cost Optimizer with a hypothetical agent harness."""
2
+
3
+ from aco import AgentCostOptimizer
4
+ from aco.config import ACOConfig
5
+
6
+
7
+ def example_agent_harness():
8
+ """Example of how to bolt ACO onto any agent harness."""
9
+
10
+ # Initialize optimizer
11
+ config = ACOConfig.from_yaml("config.yaml")
12
+ optimizer = AgentCostOptimizer(config)
13
+
14
+ # Incoming user request
15
+ user_request = "Write a Python script to fetch data from an API and cache it in Redis"
16
+
17
+ # Build run state from current agent state
18
+ run_state = {
19
+ "trace_id": "agent-run-12345",
20
+ "current_cost": 0.0,
21
+ "planned_tools": [
22
+ ("search", {"query": "redis python client"}),
23
+ ("fetch", {"url": "https://api.example.com/docs"}),
24
+ ("code_execution", {"code": "test script"}),
25
+ ],
26
+ "previous_tool_calls": [],
27
+ "step_number": 1,
28
+ "total_steps": 3,
29
+ "is_irreversible": False,
30
+ "context_pieces": {
31
+ "system_rules": "You are a coding assistant.",
32
+ "tool_descriptions": "Available tools: search, fetch, code_execution",
33
+ "user_preferences": "Prefer Python 3.11+, type hints, async where possible",
34
+ "recent_messages": "User: Write a Python script...",
35
+ },
36
+ "retrieved_docs": [],
37
+ "routing_mode": "cascade",
38
+ }
39
+
40
+ # Call optimizer before executing
41
+ decision = optimizer.optimize(user_request, run_state)
42
+
43
+ print(f"Trace ID: {decision.trace_id}")
44
+ print(f"Selected Model: {decision.routing_decision.model_id} (tier {decision.routing_decision.tier})")
45
+ print(f"Estimated Cost: ${decision.estimated_cost:.4f}")
46
+ print(f"Estimated Latency: {decision.estimated_latency_ms:.0f}ms")
47
+ print(f"Confidence: {decision.confidence:.2f}")
48
+ print()
49
+
50
+ # Apply tool gate decisions
51
+ print("Tool Decisions:")
52
+ for td in decision.tool_decisions:
53
+ print(f" {td.tool_name}: {td.decision.value} (reason: {td.reasoning})")
54
+
55
+ # Apply context budget
56
+ if decision.context_budget:
57
+ print(f"\nContext Budget: {decision.context_budget.total_budget_tokens} tokens")
58
+ print(f" Cache prefix: {decision.context_budget.cache_prefix_tokens} tokens")
59
+ print(f" Dynamic suffix: {decision.context_budget.dynamic_suffix_tokens} tokens")
60
+ if decision.context_budget.omitted_sources:
61
+ print(f" Omitted: {[s.name for s in decision.context_budget.omitted_sources]}")
62
+
63
+ # Apply cache layout
64
+ if decision.prompt_layout:
65
+ print(f"\nCache Layout:")
66
+ print(f" Cold cost: ${decision.prompt_layout.estimated_cold_cost:.4f}")
67
+ print(f" Warm cost: ${decision.prompt_layout.estimated_warm_cost:.4f}")
68
+ print(f" Cache discount: ${decision.prompt_layout.cache_discount:.4f}")
69
+
70
+ # Check meta-tool
71
+ if decision.meta_tool_match:
72
+ print(f"\nMeta-Tool Match: {decision.meta_tool_match['meta_tool_id']}")
73
+ print(f" Estimated savings: ${decision.meta_tool_match['estimated_cost_savings']:.4f}")
74
+
75
+ # Check doom assessment
76
+ if decision.doom_assessment:
77
+ print(f"\nDoom Assessment: {decision.doom_assessment.action.value}")
78
+ print(f" Confidence: {decision.doom_assessment.confidence:.2f}")
79
+ print(f" Signals: {decision.doom_assessment.signals_triggered}")
80
+
81
+ # Check verifier
82
+ if decision.verifier_decision:
83
+ print(f"\nVerifier: {decision.verifier_decision.decision.value}")
84
+ print(f" Checks: {decision.verifier_decision.checks}")
85
+ print(f" Cost: ${decision.verifier_decision.estimated_verifier_cost:.4f}")
86
+
87
+ # After execution, record step and finalize
88
+ from aco.trace_schema import ModelCall, Outcome
89
+
90
+ model_call = ModelCall(
91
+ model_id=decision.routing_decision.model_id,
92
+ provider="cloud",
93
+ input_tokens=2048,
94
+ output_tokens=512,
95
+ cost_per_1k_input=0.003,
96
+ cost_per_1k_output=0.006,
97
+ )
98
+
99
+ optimizer.record_step(
100
+ trace_id=decision.trace_id,
101
+ model_call=model_call,
102
+ context_size_tokens=2048,
103
+ step_outcome=Outcome.SUCCESS,
104
+ )
105
+
106
+ # Finalize
107
+ trace = optimizer.finalize_trace(
108
+ trace_id=decision.trace_id,
109
+ outcome=Outcome.SUCCESS,
110
+ user_satisfaction=0.95,
111
+ )
112
+
113
+ print(f"\nTrace finalized. Total cost: ${trace.total_cost_computed:.4f}")
114
+ print(f"Cost saved vs frontier: ${trace.total_cost_saved_vs_frontier:.4f}")
115
+
116
+
117
+ if __name__ == "__main__":
118
+ example_agent_harness()