-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_claude.py
More file actions
1802 lines (1452 loc) · 97.1 KB
/
client_claude.py
File metadata and controls
1802 lines (1452 loc) · 97.1 KB
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
# client_claude.py - pracovna verzie pre uz skratene odpovede z tools + rozhodovanie o pouziti tools
from __future__ import annotations
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage
import asyncio
import os
import traceback
import tiktoken
import json
import time
from typing import Union, Optional, List, Dict, Any, Type
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import LLMResult
from mcp_use import MCPAgent, MCPClient
# Načítanie environment variables
load_dotenv()
class McpFunctionRouter:
"""OpenAI Function Calling Router for decision-making"""
def __init__(self, openai_api_key: str):
self.decision_llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0,
api_key=openai_api_key
)
self.last_mcp_intent = None # Track the last intent
async def should_use_mcp(self, message: str) -> bool:
"""Decides using simple OpenAI completion whether to use MCP, with context awareness"""
try:
# Get configurable keywords from environment variables
ecommerce_keywords = os.getenv("ECOMMERCE_KEYWORDS", "product,discount,category,search,create,update,delete,set,order,top,price,available,sale,ship,shipping,delivery,europe,country,international").split(',')
general_keywords = os.getenv("GENERAL_KEYWORDS", "how,what is,explain,why,hello,math,weather,time").split(',')
decision_prompt = f"""You are a routing assistant for e-commerce MCP tools. Analyze the user's message and decide if e-commerce tools are needed.
Respond with exactly "YES" if the user wants to:
- Search for products, prices, availability, sizes, colors, discounts
- Questions like "Do you have...?", "What products...?", "Show me..."
- E-commerce related queries about the store
- Follow-up responses like "yes", "no", "more details", "tell me more" when previous context exists
- Key words: {', '.join(ecommerce_keywords)}
Respond with exactly "NO" for:
- General questions, greetings, mathematical operations
- Theoretical discussions or explanations
- Questions not related to shopping or products (when no shopping context exists)
- Key words: {', '.join(general_keywords)}
IMPORTANT: If the previous context was shopping-related ('{self.last_mcp_intent}'), then short responses like "yes", "no", "more", "details" should be treated as follow-ups to that shopping conversation.
User message: "{message}"
Response (YES or NO):"""
response = await self.decision_llm.ainvoke([
SystemMessage(content=decision_prompt),
HumanMessage(content=message)
])
# Extract the decision from the response
decision_text = response.content.strip().upper()
used_mcp = decision_text.startswith("YES")
# Update last intent if MCP tools are used
if used_mcp:
self.last_mcp_intent = message if not self.last_mcp_intent else f"{self.last_mcp_intent}, {message}"
elif self.last_mcp_intent and any(
keyword in message.lower() for keyword in ['under', 'price', 'euro', 'cost', 'top', 'yes', 'no', 'more', 'details', 'tell me', 'show me']):
# Treat follow-up responses as MCP if context exists
used_mcp = True
print(f"🔄 Context-aware decision: '{message}' treated as follow-up to '{self.last_mcp_intent}'")
# Fallback keyword check (English only)
if not used_mcp:
message_lower = message.lower()
if any(keyword.strip() in message_lower for keyword in ecommerce_keywords):
used_mcp = True
elif any(keyword.strip() in message_lower for keyword in general_keywords):
used_mcp = False
print(f"🧠 Decision: Use MCP tools = {used_mcp}")
return used_mcp
except Exception as e:
print(f"⚠️ Decision error: {e}")
traceback.print_exc()
# Fallback decision with configurable keywords
message_lower = message.lower()
ecommerce_keywords = os.getenv("ECOMMERCE_KEYWORDS", "product,discount,category,search,create,update,delete,set,order,top,price,available,sale,ship,shipping,delivery,europe,country,international").split(',')
used_mcp = any(keyword.strip() in message_lower for keyword in ecommerce_keywords)
print(f"🧠 Fallback decision: Use MCP tools = {used_mcp}")
return used_mcp
class TokenLimitManager:
"""Správca limitov tokenov"""
def __init__(self):
# Načítanie limitov z .env
self.max_input_tokens_per_call = int(os.getenv("MAX_INPUT_TOKENS_PER_CALL", "12000"))
self.max_output_tokens_per_call = int(os.getenv("MAX_OUTPUT_TOKENS_PER_CALL", "4000"))
self.max_total_input_tokens_per_interaction = int(os.getenv("MAX_TOTAL_INPUT_TOKENS_PER_INTERACTION", "40000"))
self.max_total_output_tokens_per_interaction = int(
os.getenv("MAX_TOTAL_OUTPUT_TOKENS_PER_INTERACTION", "15000"))
self.max_llm_calls_per_interaction = int(os.getenv("MAX_LLM_CALLS_PER_INTERACTION", "12"))
# === NOVÉ: Smart truncation nastavenia ===
self.tool_response_truncation_threshold = int(os.getenv("TOOL_RESPONSE_TRUNCATION_THRESHOLD", "8000"))
self.max_products_in_truncated_response = int(os.getenv("MAX_PRODUCTS_IN_TRUNCATED_RESPONSE", "10"))
self.max_lines_in_truncated_text = int(os.getenv("MAX_LINES_IN_TRUNCATED_TEXT", "50"))
self.chars_per_token_ratio = float(os.getenv("CHARS_PER_TOKEN_RATIO", "3.5"))
# Preemptívny cleanup nastavenia
self.preemptive_cleanup_threshold = int(os.getenv("PREEMPTIVE_CLEANUP_THRESHOLD", "5000"))
self.nuclear_cleanup_threshold = int(os.getenv("NUCLEAR_CLEANUP_THRESHOLD", "8000"))
# Memory management nastavenia - agresívnejšie
self.max_conversation_messages = int(os.getenv("MAX_CONVERSATION_MESSAGES", "4"))
self.memory_cleanup_threshold = float(os.getenv("MEMORY_CLEANUP_THRESHOLD", "0.5")) # 50% limitu
self.aggressive_cleanup_threshold = float(
os.getenv("AGGRESSIVE_CLEANUP_THRESHOLD", "0.8")) # 80% pre extra cleanup
# Počítadlá pre aktuálnu interakciu
self.current_interaction_input_tokens = 0
self.current_interaction_output_tokens = 0
self.current_interaction_llm_calls = 0
self.encoding = tiktoken.get_encoding("cl100k_base")
print(f"📋 Token Limits nastavené:")
print(f" • Max input tokens/call: {self.max_input_tokens_per_call:,}")
print(f" • Max output tokens/call: {self.max_output_tokens_per_call:,}")
print(f" • Max total input/interaction: {self.max_total_input_tokens_per_interaction:,}")
print(f" • Max total output/interaction: {self.max_total_output_tokens_per_interaction:,}")
print(f" • Max LLM calls/interaction: {self.max_llm_calls_per_interaction}")
print(f" • Max conversation messages: {self.max_conversation_messages}")
print(f" • Memory cleanup threshold: {self.memory_cleanup_threshold * 100:.0f}%")
print(f" • Aggressive cleanup threshold: {self.aggressive_cleanup_threshold * 100:.0f}%")
print(f" • Preemptive cleanup threshold: {self.preemptive_cleanup_threshold:,} tokenov")
print(f" • Nuclear cleanup threshold: {self.nuclear_cleanup_threshold:,} tokenov")
# === NOVÉ: Vypíš truncation nastavenia ===
print(f"\n🔧 Smart Tool Response Truncation:")
print(f" • Truncation threshold: {self.tool_response_truncation_threshold:,} tokenov")
print(f" • Max products po skrátení: {self.max_products_in_truncated_response}")
print(f" • Max riadkov po skrátení: {self.max_lines_in_truncated_text}")
print(f" • Chars/token ratio: {self.chars_per_token_ratio}")
def get_dynamic_truncation_threshold(self, current_input_tokens: int) -> int:
"""Dynamicky upraví threshold na základe aktuálneho využitia"""
usage_percent = current_input_tokens / self.max_total_input_tokens_per_interaction
if usage_percent > 0.8: # Ak nad 80%, buď agresívnejší
dynamic_threshold = int(self.tool_response_truncation_threshold * 0.6) # 60% z normálu
print(f"🔥 AGRESÍVNY truncation threshold: {dynamic_threshold:,} (usage: {usage_percent:.1%})")
return dynamic_threshold
elif usage_percent > 0.6: # Ak nad 60%, mierne agresívnejší
dynamic_threshold = int(self.tool_response_truncation_threshold * 0.8) # 80% z normálu
print(f"⚡ ZVÝŠENÝ truncation threshold: {dynamic_threshold:,} (usage: {usage_percent:.1%})")
return dynamic_threshold
else:
print(
f"✅ NORMÁLNY truncation threshold: {self.tool_response_truncation_threshold:,} (usage: {usage_percent:.1%})")
return self.tool_response_truncation_threshold
def smart_truncate_tool_response(self, tool_output: str, current_input_tokens: int = 0) -> str:
"""Inteligentne skráti tool response na najdôležitejšie časti - s dynamickým threshold"""
# 🆕 NOVÉ: Použiť dynamický threshold
if current_input_tokens > 0:
dynamic_threshold = self.get_dynamic_truncation_threshold(current_input_tokens)
else:
dynamic_threshold = self.tool_response_truncation_threshold
current_tokens = len(self.encoding.encode(tool_output))
if current_tokens <= dynamic_threshold: # ← Zmena z self.tool_response_truncation_threshold
return tool_output
print(f"🔧 Tool response príliš veľký ({current_tokens:,} tokenov), spúšťam smart truncation...")
print(f" • Dynamic threshold: {dynamic_threshold:,} tokenov") # ← Zmena
try:
# === POKUS 1: JSON TRUNCATION ===
data = json.loads(tool_output)
if 'products' in data and isinstance(data['products'], list):
products = data['products']
original_count = len(products)
print(
f"🔍 JSON DEBUG: original_count={original_count}, max_allowed={self.max_products_in_truncated_response}")
if original_count > self.max_products_in_truncated_response or current_tokens > dynamic_threshold:
# Skráť na TOP výsledky
data['products'] = products[:self.max_products_in_truncated_response]
data['truncated'] = True
data['original_count'] = original_count
data[
'truncated_message'] = f"Zobrazených TOP {self.max_products_in_truncated_response} z {original_count} produktov"
truncated_output = json.dumps(data, ensure_ascii=False, separators=(',', ':'))
new_tokens = len(self.encoding.encode(truncated_output))
print(f"✅ JSON skrátené: {original_count} → {self.max_products_in_truncated_response} produktov")
print(f"✅ Tokeny znížené: {current_tokens:,} → {new_tokens:,}")
return truncated_output
# Ak má iné JSON polia ako 'products'
elif 'results' in data and isinstance(data['results'], list):
results = data['results']
original_count = len(results)
if original_count > self.max_products_in_truncated_response:
data['results'] = results[:self.max_products_in_truncated_response]
data['truncated'] = True
data['original_count'] = original_count
data[
'truncated_message'] = f"Zobrazených TOP {self.max_products_in_truncated_response} z {original_count} výsledkov"
truncated_output = json.dumps(data, ensure_ascii=False, separators=(',', ':'))
new_tokens = len(self.encoding.encode(truncated_output))
print(f"✅ JSON results skrátené: {original_count} → {self.max_products_in_truncated_response}")
print(f"✅ Tokeny znížené: {current_tokens:,} → {new_tokens:,}")
return truncated_output
except json.JSONDecodeError:
pass # Pokračuj s textovým truncation
# === POKUS 2: TEXT TRUNCATION ===
lines = tool_output.split('\n')
if len(lines) > self.max_lines_in_truncated_text:
truncated_lines = lines[:self.max_lines_in_truncated_text]
truncated_lines.append(f"\n... (skrátené z {len(lines)} na {self.max_lines_in_truncated_text} riadkov)")
truncated_output = '\n'.join(truncated_lines)
new_tokens = len(self.encoding.encode(truncated_output))
print(f"✅ Text skrátený: {len(lines)} → {self.max_lines_in_truncated_text} riadkov")
print(f"✅ Tokeny znížené: {current_tokens:,} → {new_tokens:,}")
return truncated_output
# === POKUS 3: HARD TRUNCATION ===
target_chars = int(dynamic_threshold * 0.7 * self.chars_per_token_ratio)
truncated = tool_output[:target_chars]
truncated += f"\n\n... (hard truncation: skrátené z {current_tokens:,} tokenov na ~{dynamic_threshold * 0.7:,})"
new_tokens = len(self.encoding.encode(truncated))
print(f"✅ Hard truncation: {current_tokens:,} → {new_tokens:,} tokenov")
return truncated
def reset_interaction(self):
"""Reset počítadiel pre novú interakciu"""
self.current_interaction_input_tokens = 0
self.current_interaction_output_tokens = 0
self.current_interaction_llm_calls = 0
def can_make_llm_call(self, estimated_input_tokens: int = 0) -> tuple[bool, str]:
"""Kontroluje či môžeme robiť ďalší LLM call"""
# Kontrola počtu LLM calls
if self.current_interaction_llm_calls >= self.max_llm_calls_per_interaction:
return False, f"Dosiahnutý limit LLM calls ({self.max_llm_calls_per_interaction})"
# Kontrola input tokenov pre call
if estimated_input_tokens > self.max_input_tokens_per_call:
return False, f"Input pre call príliš veľký ({estimated_input_tokens:,} > {self.max_input_tokens_per_call:,})"
# Kontrola celkových input tokenov pre interakciu
if (
self.current_interaction_input_tokens + estimated_input_tokens) > self.max_total_input_tokens_per_interaction:
return False, f"Celkové input tokeny príliš veľké ({self.current_interaction_input_tokens + estimated_input_tokens:,} > {self.max_total_input_tokens_per_interaction:,})"
return True, "OK"
def should_cleanup_memory(self, estimated_input_tokens: int = 0) -> tuple[bool, str]:
"""Kontroluje či treba vyčistiť memory - citlivejšie nastavenia"""
# Základný threshold pre štandardný cleanup
threshold_tokens = int(self.max_input_tokens_per_call * self.memory_cleanup_threshold)
# Agresívny threshold pre extra cleanup
aggressive_threshold = int(self.max_input_tokens_per_call * self.aggressive_cleanup_threshold)
if estimated_input_tokens > aggressive_threshold:
return True, "aggressive"
elif estimated_input_tokens > threshold_tokens:
return True, "standard"
return False, "none"
def cleanup_agent_memory(self, agent, cleanup_type: str = "standard") -> int:
"""Vyčistí memory agenta - agresívnejšie podľa typu"""
if not hasattr(agent, 'memory') or not hasattr(agent.memory, 'chat_memory'):
return 0
messages = agent.memory.chat_memory.messages
original_count = len(messages)
if original_count <= 1:
return 0
# Spočítaj tokeny pred cleanup
tokens_before = sum(len(self.encoding.encode(str(msg.content))) for msg in messages)
if cleanup_type == "aggressive":
# Extra agresívny cleanup - nechaj len poslednú správu
agent.memory.chat_memory.messages = messages[-1:]
print(f"🧹🧹 AGGRESSIVE cleanup: nechané len 1 správa")
elif cleanup_type == "standard":
# Štandardný cleanup - nechaj 2-3 posledné správy
keep_count = min(2, original_count)
agent.memory.chat_memory.messages = messages[-keep_count:]
print(f"🧹 Standard cleanup: nechané {len(agent.memory.chat_memory.messages)} správy")
# Spočítaj tokeny po cleanup
tokens_after = sum(len(self.encoding.encode(str(msg.content))) for msg in agent.memory.chat_memory.messages)
# Ak stále príliš veľa tokenov, extra agresívny cleanup
if tokens_after > self.nuclear_cleanup_threshold: # ← Zmena z 8000 na .env premennú
agent.memory.chat_memory.messages = []
tokens_after = 0
print(
f"🧹🧹🧹 FORCE NUCLEAR: vymazané všetko kvôli {tokens_after:,} tokenov (threshold: {self.nuclear_cleanup_threshold:,})")
removed_messages = original_count - len(agent.memory.chat_memory.messages)
removed_tokens = tokens_before - tokens_after
if removed_messages > 0:
print(
f"🧹 Memory cleanup ({cleanup_type}): odstránených {removed_messages} správ, ušetrených {removed_tokens:,} tokenov")
return removed_tokens
def can_accept_output(self, output_tokens: int) -> tuple[bool, str]:
"""Kontroluje či môžeme prijať output"""
# Kontrola output tokenov pre call
if output_tokens > self.max_output_tokens_per_call:
return False, f"Output pre call príliš veľký ({output_tokens:,} > {self.max_output_tokens_per_call:,})"
# Kontrola celkových output tokenov pre interakciu
if (self.current_interaction_output_tokens + output_tokens) > self.max_total_output_tokens_per_interaction:
return False, f"Celkové output tokeny príliš veľké ({self.current_interaction_output_tokens + output_tokens:,} > {self.max_total_output_tokens_per_interaction:,})"
return True, "OK"
def add_llm_call(self, input_tokens: int, output_tokens: int):
"""Pridá LLM call do počítadiel"""
self.current_interaction_input_tokens += input_tokens
self.current_interaction_output_tokens += output_tokens
self.current_interaction_llm_calls += 1
class AgentTokenCounterCallback(BaseCallbackHandler):
"""Callback handler pre počítanie tokenov v agentoch"""
def __init__(self, token_counter, token_limit_manager):
self.token_counter = token_counter
self.token_limit_manager = token_limit_manager
self.encoding = tiktoken.get_encoding("cl100k_base")
self.current_step_tokens = {'input': 0, 'output': 0}
self.should_stop = False
self.agent_ref = None
self.cleanup_attempts = 0
def set_agent_reference(self, agent):
"""Nastaví referenciu na agent pre memory management"""
self.agent_ref = agent
def on_llm_start(self, serialized: Dict[str, Any], prompts: List[str], **kwargs) -> None:
"""Zachytí začiatok LLM volania s token counting PRED cleanup"""
if prompts:
# === POČÍTANIE TOKENOV PRED CLEANUP ===
input_text = ' '.join(prompts)
original_input_tokens = len(self.encoding.encode(input_text))
print(f"🔍 LLM START: Original input {original_input_tokens:,} tokenov")
# === OKAMŽITÝ CLEANUP PRED KAŽDÝM LLM CALL ===
cleanup_performed = False
if self.agent_ref and hasattr(self.agent_ref, 'memory') and hasattr(self.agent_ref.memory, 'chat_memory'):
messages = self.agent_ref.memory.chat_memory.messages
if len(messages) > 0:
memory_tokens = sum(len(self.encoding.encode(str(msg.content))) for msg in messages)
print(f"🧠 Memory check: {len(messages)} správ, {memory_tokens:,} tokenov")
# AGRESÍVNY cleanup - vymaž všetko ak viac ako nuclear threshold
if memory_tokens > self.token_limit_manager.nuclear_cleanup_threshold:
self.agent_ref.memory.chat_memory.messages = []
print(f"🧹🧹🧹 OKAMŽITÝ nuclear cleanup: {memory_tokens:,} tokenov vymazaných!")
cleanup_performed = True
# Menej agresívny cleanup ak viac ako 2 správy
elif len(messages) > 1:
self.agent_ref.memory.chat_memory.messages = messages[-1:]
print(f"🧹 Standard cleanup: nechané len 1 správa")
cleanup_performed = True
# === PREPOČÍTANIE TOKENOV PO CLEANUP ===
if cleanup_performed:
# Prepočítaj input tokeny po cleanup
new_input_text = ' '.join(prompts)
final_input_tokens = len(self.encoding.encode(new_input_text))
print(f"✅ Po cleanup: input znížený na {final_input_tokens:,} tokenov")
else:
final_input_tokens = original_input_tokens
# === ULOŽENIE DO TOKEN COUNTER (POUŽÍVA ORIGINÁLNE TOKENY) ===
self.current_step_tokens['input'] = original_input_tokens # ← KĽÚČOVÉ!
# === PÔVODNÁ LOGIKA PRE LLM LIMITS (POUŽÍVA FINÁLNE TOKENY) ===
# Kontrola či potrebujeme memory cleanup (backup)
needs_cleanup, cleanup_type = self.token_limit_manager.should_cleanup_memory(final_input_tokens)
if needs_cleanup and self.agent_ref:
print(
f"🧹 BACKUP cleanup: Input príliš veľký ({final_input_tokens:,} tokenov), spúšťam {cleanup_type} cleanup...")
removed_tokens = self.token_limit_manager.cleanup_agent_memory(self.agent_ref, cleanup_type)
self.cleanup_attempts += 1
if removed_tokens > 0:
estimated_new_tokens = max(final_input_tokens - (removed_tokens // 2),
self.token_limit_manager.nuclear_cleanup_threshold)
print(f"✅ Po backup cleanup: odhadovaných {estimated_new_tokens:,} tokenov")
final_input_tokens = estimated_new_tokens
# Kontrola limitov po cleanup
can_proceed, reason = self.token_limit_manager.can_make_llm_call(final_input_tokens)
if not can_proceed:
print(f"\n⚠️ LLM call zastavený: {reason}")
# Posledný pokus - nuclear cleanup
if self.cleanup_attempts < 2 and self.agent_ref:
print("🧹🧹🧹 Posledný pokus - nuclear cleanup...")
self.token_limit_manager.cleanup_agent_memory(self.agent_ref, "aggressive")
estimated_minimal = self.token_limit_manager.nuclear_cleanup_threshold
can_proceed_final, _ = self.token_limit_manager.can_make_llm_call(estimated_minimal)
if can_proceed_final:
print(f"✅ Po nuclear cleanup: pokračujem s {estimated_minimal:,} tokenmi")
final_input_tokens = estimated_minimal
else:
self.should_stop = True
return
else:
self.should_stop = True
return
def on_llm_end(self, response: LLMResult, **kwargs) -> None:
"""Zachytí ukončenie LLM volania - OPRAVENÉ získavanie real token data"""
if self.should_stop:
self.should_stop = False
self.cleanup_attempts = 0
return
# === AGRESÍVNE HĽADANIE REAL TOKEN DATA ===
real_input = 0
real_output = 0
found_real_data = False
print(f"🔍 HĽADÁM real token data vo všetkých možných miestach:")
# === NOVÉ: Hľadaj v message objektoch ===
if response.generations:
for i, gen_list in enumerate(response.generations):
for j, gen in enumerate(gen_list):
if hasattr(gen, 'message'):
message = gen.message
# 1. usage_metadata (OPRAVENÉ - je to dict!)
if hasattr(message, 'usage_metadata') and message.usage_metadata:
usage = message.usage_metadata
if 'input_tokens' in usage: # ← OPRAVENÉ!
real_input = usage['input_tokens']
real_output = usage['output_tokens']
print(f" ✅ Nájdené v message.usage_metadata: {real_input:,} + {real_output:,}")
found_real_data = True
break
# 2. response_metadata
if not found_real_data and hasattr(message, 'response_metadata') and message.response_metadata:
metadata = message.response_metadata
if 'usage' in metadata:
usage = metadata['usage']
real_input = usage.get('input_tokens', 0)
real_output = usage.get('output_tokens', 0)
if real_input > 0 or real_output > 0:
print(
f" ✅ Nájdené v message.response_metadata.usage: {real_input:,} + {real_output:,}")
found_real_data = True
break
if found_real_data:
break
if found_real_data:
break
# === AK MÁME REAL DATA, POUŽIJEME ICH ===
if found_real_data and (real_input > 0 or real_output > 0):
print(f"🎯 POUŽÍVAM REAL API tokens: {real_input:,} input + {real_output:,} output")
self.current_step_tokens['input'] = real_input
self.current_step_tokens['output'] = real_output
# Pridaj do limitov a counters
self.token_limit_manager.add_llm_call(real_input, real_output)
if hasattr(self.token_counter, 'add_llm_call'):
self.token_counter.add_llm_call(real_input, real_output)
# Reset cleanup attempts
self.cleanup_attempts = 0
return # ← KĽÚČOVÉ: Skonči tu ak máš real data!
# === BACKUP: TIKTOKEN ESTIMATE ===
print("⚠️ Real token data nenájdené, používam tiktoken estimate")
if response.generations:
# Počítanie output tokenov
output_text = ''
for generation in response.generations:
for gen in generation:
if hasattr(gen, 'text'):
output_text += gen.text
output_tokens = len(self.encoding.encode(output_text))
# Kontrola output limitov
can_accept, reason = self.token_limit_manager.can_accept_output(output_tokens)
if not can_accept:
print(f"\n⚠️ Output obmedzený: {reason}")
return
self.current_step_tokens['output'] = output_tokens
# Pridanie do limitov a token counter (používaj pôvodný input z on_llm_start)
self.token_limit_manager.add_llm_call(
self.current_step_tokens['input'],
output_tokens
)
if hasattr(self.token_counter, 'add_llm_call'):
self.token_counter.add_llm_call(
self.current_step_tokens['input'],
output_tokens
)
# Reset cleanup attempts po úspešnom LLM call
self.cleanup_attempts = 0
def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwargs) -> None:
"""Zachytí začiatok tool volania"""
tool_name = serialized.get('name', 'unknown_tool')
if hasattr(self.token_counter, 'add_tool_start'):
self.token_counter.add_tool_start(tool_name, input_str)
def on_tool_end(self, output: str, **kwargs) -> None:
"""Zachytí ukončenie tool volania - BEZ duplicitného truncation"""
# Tool interception sa už robí v SmartMCPAgent._setup_langchain_tool_interception()
# Takže tu už nemusíme robiť truncation, len počítanie tokenov
if hasattr(self.token_counter, 'add_tool_end'):
self.token_counter.add_tool_end(output)
# Spočítaj veľkosť tool output (už po truncation)
tool_output_tokens = len(self.encoding.encode(output))
# === ZAPOČÍTANIE TOOL TOKENOV DO CLAUDE CONTEXT ===
# Tool responses sa SKUTOČNE rátajú do Claude input tokenov
claude_context_tokens = int(tool_output_tokens * 1.2) # 20% overhead
self.token_limit_manager.current_interaction_input_tokens += claude_context_tokens
print(f"🔧 Tool response: {tool_output_tokens:,} tokenov")
print(f" • Pridané do Claude context: {claude_context_tokens:,} tokenov")
print(f" • Celkové input tokeny: {self.token_limit_manager.current_interaction_input_tokens:,}")
print(f" • Preemptive threshold: {self.token_limit_manager.preemptive_cleanup_threshold:,}")
print(f" • Nuclear threshold: {self.token_limit_manager.nuclear_cleanup_threshold:,}")
print(f" • Agent ref exists: {self.agent_ref is not None}")
# === PREEMPTÍVNY CLEANUP PRE VEĽKÉ TOOL RESPONSES ===
# Používaj finálne tokeny (po truncation) pre cleanup rozhodnutia
if tool_output_tokens > self.token_limit_manager.preemptive_cleanup_threshold and self.agent_ref:
print(f"🧹⚡ Preemptívny cleanup: tool response {tool_output_tokens:,} tokenov")
removed = self.token_limit_manager.cleanup_agent_memory(self.agent_ref, "standard")
if removed > 0:
print(f"✅ Memory vyčistená pred ďalším krokom")
# === NUCLEAR CLEANUP PRE EXTRA VEĽKÉ TOOL RESPONSES ===
# Aj po truncation môže byť response stále veľký
if tool_output_tokens > self.token_limit_manager.nuclear_cleanup_threshold and self.agent_ref:
print(f"🧹🧹🧹 NUCLEAR cleanup: tool response {tool_output_tokens:,} tokenov!")
self.token_limit_manager.cleanup_agent_memory(self.agent_ref, "aggressive")
# Úplne vymaž memory
if hasattr(self.agent_ref, 'memory') and hasattr(self.agent_ref.memory, 'chat_memory'):
self.agent_ref.memory.chat_memory.messages = []
print("✅ Úplné vymazanie conversation history")
# === EXTRA OCHRANA: Kontrola celkových limitov ===
input_usage_percent = (self.token_limit_manager.current_interaction_input_tokens /
self.token_limit_manager.max_total_input_tokens_per_interaction) * 100
if input_usage_percent > 90: # Ak je využitie nad 90%
print(f"⚠️ VAROVANIE: Input tokeny na {input_usage_percent:.1f}% limitu!")
print(f" • Aktuálne: {self.token_limit_manager.current_interaction_input_tokens:,}")
print(f" • Limit: {self.token_limit_manager.max_total_input_tokens_per_interaction:,}")
# Extra agresívny cleanup ak blízko limitu
if self.agent_ref and hasattr(self.agent_ref, 'memory') and hasattr(self.agent_ref.memory, 'chat_memory'):
self.agent_ref.memory.chat_memory.messages = []
print("🧹🧹🧹 EMERGENCY cleanup: vymazaná celá conversation history!")
class AdvancedTokenCounter:
def __init__(self, token_limit_manager):
# Claude používa cl100k_base encoding (rovnaký ako GPT-4)
self.encoding = tiktoken.get_encoding("cl100k_base")
self.token_limit_manager = token_limit_manager
self.reset_session()
def reset_session(self):
"""Reset počítadla pre celú session"""
self.session_input_tokens = 0
self.session_output_tokens = 0
self.conversation_history = []
self.current_interaction = {}
self.current_tool_call = {}
def count_tokens(self, text: str) -> int:
"""Spočíta tokeny v texte"""
if not text or not isinstance(text, str):
return 0
return len(self.encoding.encode(text))
def extract_text_from_result(self, result) -> str:
"""Extrahuje text z komplexnej štruktúry agenta"""
if isinstance(result, str):
return result
elif isinstance(result, list):
text_parts = []
for item in result:
if isinstance(item, dict):
if 'text' in item:
text_parts.append(str(item['text']))
elif 'content' in item:
text_parts.append(str(item['content']))
elif isinstance(item, str):
text_parts.append(item)
return ' '.join(text_parts)
elif isinstance(result, dict):
if 'text' in result:
return str(result['text'])
elif 'content' in result:
return str(result['content'])
else:
return str(result)
else:
return str(result)
def start_interaction(self, user_input: str):
"""Začne novú interakciu"""
# Reset token limit manager
self.token_limit_manager.reset_interaction()
self.current_interaction = {
'user_input': user_input,
'input_tokens': self.count_tokens(user_input),
'llm_calls': [],
'tool_calls': [],
'final_response': '',
'total_input_tokens': 0,
'total_output_tokens': 0,
'api_calls': 0,
'duration': 0,
'start_time': time.time(),
'limits_hit': [],
'memory_cleanups': 0
}
def add_llm_call(self, input_tokens: int, output_tokens: int):
"""Pridá LLM call do súčasnej interakcie"""
self.current_interaction['llm_calls'].append({
'input_tokens': input_tokens,
'output_tokens': output_tokens
})
self.current_interaction['api_calls'] += 1
def add_tool_start(self, tool_name: str, tool_input: str):
"""Začne tool call"""
self.current_tool_call = {
'name': tool_name,
'input': tool_input,
'input_tokens': self.count_tokens(f"{tool_name}: {tool_input}"),
'output': '',
'output_tokens': 0
}
def add_tool_end(self, tool_output: str):
"""Dokončí tool call"""
self.current_tool_call['output'] = tool_output
self.current_tool_call['output_tokens'] = self.count_tokens(tool_output)
self.current_interaction['tool_calls'].append(self.current_tool_call.copy())
self.current_tool_call = {}
def finish_interaction(self, final_response):
"""Dokončí interakciu a spočíta celkové tokeny - OPRAVENÉ počítanie"""
# Extrakcia textu z komplexnej štruktúry
final_response_text = self.extract_text_from_result(final_response)
final_response_tokens = self.count_tokens(final_response_text)
self.current_interaction['final_response'] = final_response_text
self.current_interaction['duration'] = time.time() - self.current_interaction['start_time']
# === SPRÁVNE POČÍTANIE INPUT A OUTPUT TOKENOV ===
# INPUT TOKENY = všetko čo ide DO Claude
total_input = self.current_interaction['input_tokens'] # User input
# Pridaj LLM input tokeny
for llm_call in self.current_interaction['llm_calls']:
total_input += llm_call['input_tokens']
# Pridaj tool INPUT tokeny (definície nástrojov)
for tool_call in self.current_interaction['tool_calls']:
total_input += tool_call['input_tokens']
# KĽÚČOVÉ: Tool RESPONSES sa rátajú ako INPUT pre ďalší Claude call
for tool_call in self.current_interaction['tool_calls']:
total_input += tool_call['output_tokens'] # Tool output → Claude input!
# OUTPUT TOKENY = len to čo VYGENEROVAL Claude
total_output = final_response_tokens # Final response
# Pridaj LLM output tokeny (text + tool calls čo Claude vygeneroval)
for llm_call in self.current_interaction['llm_calls']:
total_output += llm_call['output_tokens']
# NIE tool responses - tie sú input pre ďalší call, nie output súčasného!
# === MANUAL TOOL DETECTION (ak tool_calls je prázdne) ===
if len(self.current_interaction['tool_calls']) == 0 and len(self.current_interaction['llm_calls']) >= 2:
# Ak vidíme veľký skok v input tokenoch medzi call-mi
first_call = self.current_interaction['llm_calls'][0]['input_tokens']
second_call = self.current_interaction['llm_calls'][1]['input_tokens']
tool_tokens_estimate = second_call - first_call - 100 # -100 pre malé zmeny
if tool_tokens_estimate > 1000: # Ak je rozdiel veľký, tool call sa stal
# Simuluj tool call
self.current_interaction['tool_calls'] = [{
'name': 'detected_tool_call',
'input_tokens': 50, # Odhad pre tool definíciu
'output_tokens': tool_tokens_estimate
}]
# Prepočítaj s detected tool call
total_input += 50 + tool_tokens_estimate # tool def + tool response
print(f"🔧 MANUAL tool detection: Odhadovaných {tool_tokens_estimate:,} tool tokenov")
# Uloženie výsledkov
self.current_interaction['total_input_tokens'] = total_input
self.current_interaction['total_output_tokens'] = total_output
# Pridanie do session počítadla
self.session_input_tokens += total_input
self.session_output_tokens += total_output
# Uloženie do histórie
self.conversation_history.append(self.current_interaction.copy())
return total_input, total_output
def print_interaction_tokens(self, input_tokens: int, output_tokens: int):
"""Vypíše detailné tokeny pre túto interakciu - OPRAVENÉ zobrazenie"""
duration = self.current_interaction.get('duration', 0)
api_calls = self.current_interaction.get('api_calls', 0)
llm_calls = len(self.current_interaction.get('llm_calls', []))
tool_calls = len(self.current_interaction.get('tool_calls', []))
print(f"\n📊 Token Usage (táto odpoveď):")
print(f" 📥 Input tokens: {input_tokens:,}")
print(f" • User input: {self.current_interaction['input_tokens']:,}")
if self.current_interaction.get('llm_calls'):
llm_input_total = sum(call['input_tokens'] for call in self.current_interaction['llm_calls'])
print(f" • LLM calls input: {llm_input_total:,}")
if self.current_interaction.get('tool_calls'):
tool_input_total = sum(tc['input_tokens'] for tc in self.current_interaction['tool_calls'])
tool_output_total = sum(tc['output_tokens'] for tc in self.current_interaction['tool_calls'])
print(f" • Tool definitions: {tool_input_total:,}")
print(f" • Tool responses (→ Claude input): {tool_output_total:,}")
print(f" 📤 Output tokens: {output_tokens:,}")
if self.current_interaction.get('llm_calls'):
llm_output_total = sum(call['output_tokens'] for call in self.current_interaction['llm_calls'])
print(f" • LLM generated text: {llm_output_total:,}")
print(f" • Final response: {self.count_tokens(self.current_interaction['final_response']):,}")
if self.current_interaction.get('tool_calls'):
print(f" 🔧 Tools used: {tool_calls}")
# Detailný breakdown nástrojov
for tool_call in self.current_interaction['tool_calls']:
tool_def_tokens = tool_call['input_tokens']
tool_response_tokens = tool_call['output_tokens']
print(f" ◦ {tool_call['name']}: {tool_def_tokens:,} def + {tool_response_tokens:,} response")
print(f" 📊 Total tokens: {input_tokens + output_tokens:,}")
print(f" ⏱️ Duration: {duration:.1f}s")
print(f" 🌐 LLM calls: {llm_calls} | Tool calls: {tool_calls}")
# Token limit status
usage_percent_input = (
self.token_limit_manager.current_interaction_input_tokens / self.token_limit_manager.max_total_input_tokens_per_interaction) * 100
usage_percent_output = (
self.token_limit_manager.current_interaction_output_tokens / self.token_limit_manager.max_total_output_tokens_per_interaction) * 100
print(f" 📋 Token Limits Status:")
print(
f" • Input: {self.token_limit_manager.current_interaction_input_tokens:,}/{self.token_limit_manager.max_total_input_tokens_per_interaction:,} ({usage_percent_input:.1f}%)")
print(
f" • Output: {self.token_limit_manager.current_interaction_output_tokens:,}/{self.token_limit_manager.max_total_output_tokens_per_interaction:,} ({usage_percent_output:.1f}%)")
print(
f" • LLM calls: {self.token_limit_manager.current_interaction_llm_calls}/{self.token_limit_manager.max_llm_calls_per_interaction}")
def print_session_tokens(self):
"""Vypíše celkové tokeny za session"""
total_tokens = self.session_input_tokens + self.session_output_tokens
total_interactions = len(self.conversation_history)
total_tool_calls = sum(len(interaction.get('tool_calls', [])) for interaction in self.conversation_history)
total_llm_calls = sum(len(interaction.get('llm_calls', [])) for interaction in self.conversation_history)
total_duration = sum(interaction.get('duration', 0) for interaction in self.conversation_history)
total_cleanups = sum(interaction.get('memory_cleanups', 0) for interaction in self.conversation_history)
print(f"\n📊 Session Token Stats:")
print(f" 📥 Total Input tokens: {self.session_input_tokens:,}")
print(f" 📤 Total Output tokens: {self.session_output_tokens:,}")
print(f" 📊 Total tokens: {total_tokens:,}")
print(f" 💬 Interactions: {total_interactions}")
print(f" 🔧 Tool calls: {total_tool_calls}")
print(f" 🌐 LLM calls: {total_llm_calls}")
print(f" 🧹 Memory cleanups: {total_cleanups}")
print(f" ⏱️ Total duration: {total_duration:.1f}s")
if total_interactions > 0:
print(f" 📈 Avg tokens/interaction: {total_tokens // total_interactions:,}")
print(f" 📈 Avg duration/interaction: {total_duration / total_interactions:.1f}s")
# Globálny rate limiter s adaptívnym delayom
last_api_call = 0
consecutive_429_errors = 0
async def rate_limited_execution(func, *args, **kwargs):
"""Wrapper pre rate limiting akejkoľvek async funkcie s adaptívnym delayom"""
global last_api_call, consecutive_429_errors
# Adaptívny delay na základe chýb
base_delay = float(os.getenv("MIN_DELAY_BETWEEN_CALLS", "1.5"))
adaptive_delay = base_delay + (consecutive_429_errors * 0.5) # Pridaj 0.5s za každú 429 chybu
adaptive_delay = min(adaptive_delay, 5.0) # Max 5 sekúnd
current_time = time.time()
time_since_last = current_time - last_api_call
if time_since_last < adaptive_delay:
wait_time = adaptive_delay - time_since_last
print(f"⏳ Rate limiting: čakám {wait_time:.1f}s (adaptive delay: {adaptive_delay:.1f}s)...")
await asyncio.sleep(wait_time)
last_api_call = time.time()
# Retry logic pre 429 chyby s exponential backoff
max_retries = 4 # Zvýšený z 3 na 4
base_wait = 3.0 # Zvýšený z 2.0 na 3.0
for attempt in range(max_retries):
try:
result = await func(*args, **kwargs)
# Úspech - reset 429 counter
consecutive_429_errors = max(0, consecutive_429_errors - 1)
return result
except Exception as e:
error_msg = str(e).lower()
if "429" in error_msg or "too many requests" in error_msg:
consecutive_429_errors += 1
wait_time = base_wait * (2 ** attempt) + (consecutive_429_errors * 2) # Extra delay pre opakované 429
print(
f"⚠️ Rate limit hit (pokus {attempt + 1}/{max_retries}, consecutive 429s: {consecutive_429_errors})")
print(f"⏳ Čakám {wait_time:.1f}s...")
await asyncio.sleep(wait_time)
continue
else:
# Iná chyba, prerušiť
raise e
# Ak sa všetky pokusy vyčerpali
consecutive_429_errors += 2 # Penalizácia za úplné zlyhanie
raise Exception(f"Rate limit exceeded after {max_retries} retries")
class SmartMCPAgent(MCPAgent):
"""MCPAgent with rate limiting and memory management - simplified for Haiku"""
def __init__(self, *args, token_callback=None, **kwargs):
super().__init__(*args, **kwargs)
self.token_callback = token_callback
self.iteration_count = 0
self.context_reset = False
if token_callback:
token_callback.set_agent_reference(self)
async def initialize(self):
"""Override initialize to setup tool interception after tools are ready"""
await super().initialize()
# Instead of MCP client interception, intercept LangChain tools
self._setup_langchain_tool_interception()
def _setup_langchain_tool_interception(self):
"""Sets up intercepting LangChain tool responses for smart truncation"""
if not hasattr(self, '_tools') or not self._tools:
print("⚠️ Tools not available for interception")
return
print(f"🔧 Intercepting {len(self._tools)} LangChain tools...")
# Intercept each tool in _tools
for i, tool in enumerate(self._tools):
if hasattr(tool, '_run'):
# Keep original method
if not hasattr(tool, '_original_run'):
tool._original_run = tool._run
# Create intercepted version
def create_intercepted_run(original_tool, original_run_method):
def intercepted_run(*args, **kwargs):
print(f"🔧 Tool call intercepted: {original_tool.name}")
# Special debug for shipping zone methods
if 'shipping_zone' in original_tool.name.lower():
print(f"🚢 SHIPPING ZONE DEBUG - {original_tool.name}:")
print(f" • Args: {args}")
print(f" • Kwargs: {kwargs}")
try:
result = original_run_method(*args, **kwargs)
# Check if result indicates an error (MCP client may return error messages instead of raising)
if isinstance(result, str):
result_lower = result.lower()
if any(term in result_lower for term in [
"error", "failed", "unable", "timeout", "connection", "invalid"
]):
print(f"🔄 Tool {original_tool.name} returned error result: {result}")
# Provide specific responses based on tool type
if 'shipping' in original_tool.name.lower():
return "Shipping information is currently unavailable due to a technical issue. For accurate shipping details and costs, please contact our customer service or check the shipping page on our website."
elif 'product' in original_tool.name.lower():
return "Product information is currently unavailable due to a technical issue. Please visit our website directly to browse our current product catalog."
else:
return f"Tool {original_tool.name} temporarily unavailable due to communication error. Please try again."
except Exception as e:
error_msg = str(e).lower()
is_json_rpc_error = any(term in error_msg for term in [
"json", "rpc", "validation", "pydantic", "field required",
"parsing json response", "error parsing"
])
if is_json_rpc_error:
print(f"🔄 JSON-RPC error in tool {original_tool.name}: {e}")
print(" Returning fallback response...")
# Provide specific responses based on tool type
if 'shipping' in original_tool.name.lower():
return "Shipping information is currently unavailable due to a technical issue. For accurate shipping details and costs, please contact our customer service or check the shipping page on our website."
elif 'product' in original_tool.name.lower():
return "Product information is currently unavailable due to a technical issue. Please visit our website directly to browse our current product catalog."