docoreai 2.3.7__tar.gz → 2.3.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. {docoreai-2.3.7 → docoreai-2.3.9}/PKG-INFO +9 -4
  2. {docoreai-2.3.7 → docoreai-2.3.9}/README.md +5 -2
  3. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/auto_patch.py +276 -16
  4. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/cli.py +33 -1
  5. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/config/db.py +112 -6
  6. docoreai-2.3.9/docore_ai/instrumentation/adapters/autogen.py +224 -0
  7. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/adapters/langchain.py +2 -2
  8. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/adapters/openai_agents.py +8 -2
  9. docoreai-2.3.9/docore_ai/instrumentation/agentic_cost/__init__.py +2 -0
  10. docoreai-2.3.9/docore_ai/instrumentation/agentic_cost/embedder.py +69 -0
  11. docoreai-2.3.9/docore_ai/instrumentation/agentic_cost/prompt_compression.py +193 -0
  12. docoreai-2.3.9/docore_ai/instrumentation/agentic_cost/tool_pruning_cache.py +818 -0
  13. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/framework_registry.py +1 -0
  14. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/resolver.py +10 -0
  15. docoreai-2.3.9/docore_ai/reasoning_capture.py +123 -0
  16. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/telemetry_local.py +88 -5
  17. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/telemetry_wp.py +30 -1
  18. docoreai-2.3.9/docore_ai/telemetry/token_utils.py +92 -0
  19. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/agentic_predictor.py +12 -0
  20. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/phase2_config_tab.py +6 -2
  21. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/model_patch_targets_sync.py +13 -10
  22. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/model_registry_sync.py +123 -49
  23. {docoreai-2.3.7 → docoreai-2.3.9}/docoreai.egg-info/SOURCES.txt +6 -0
  24. {docoreai-2.3.7 → docoreai-2.3.9}/setup.py +5 -2
  25. docoreai-2.3.7/docore_ai/telemetry/token_utils.py +0 -46
  26. {docoreai-2.3.7 → docoreai-2.3.9}/CONTRIBUTING-TESTERS.md +0 -0
  27. {docoreai-2.3.7 → docoreai-2.3.9}/DEPLOYMENT.md +0 -0
  28. {docoreai-2.3.7 → docoreai-2.3.9}/LICENSE.md +0 -0
  29. {docoreai-2.3.7 → docoreai-2.3.9}/MANIFEST.in +0 -0
  30. {docoreai-2.3.7 → docoreai-2.3.9}/api/__init__.py +0 -0
  31. {docoreai-2.3.7 → docoreai-2.3.9}/api/main.py +0 -0
  32. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/__init__.py +0 -0
  33. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/config/__init__.py +0 -0
  34. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/config/init_db_async.py +0 -0
  35. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/fusion_log_server.py +0 -0
  36. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/init_engine.py +0 -0
  37. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/__init__.py +0 -0
  38. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/adapters/__init__.py +0 -0
  39. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/adapters/base.py +0 -0
  40. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/adapters/crewai.py +0 -0
  41. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/instrumentation/context_manager.py +0 -0
  42. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/model.py +0 -0
  43. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/patch_manager.py +0 -0
  44. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/patcher.py +0 -0
  45. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/prompt_updates/__init__.py +0 -0
  46. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/prompt_updates/check_prompt_update.py +0 -0
  47. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/prompt_updates/prompt_loader.py +0 -0
  48. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/providers/__init__.py +0 -0
  49. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/providers/bedrock_adapter.py +0 -0
  50. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/providers/ollama.py +0 -0
  51. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/proxy.py +0 -0
  52. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/pth_installer.py +0 -0
  53. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/pth_installer_new.py +0 -0
  54. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/runtime/__init__.py +0 -0
  55. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/runtime/settings.py +0 -0
  56. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/__init__.py +0 -0
  57. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/comparator/base.py +0 -0
  58. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/comparator/llama_cpp_comparator.py +0 -0
  59. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/core/alert_emitter.py +0 -0
  60. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/core/confidence_engine.py +0 -0
  61. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/core/derived_gate.py +0 -0
  62. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/core/governance_dispatcher.py +0 -0
  63. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/core/policy.py +0 -0
  64. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/detectors/__init__.py +0 -0
  65. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/detectors/content_policy.py +0 -0
  66. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/detectors/pii_detector.py +0 -0
  67. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/export/exporter_map.py +0 -0
  68. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/export/sdk_hooks.py +0 -0
  69. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/lineage/fingerprint.py +0 -0
  70. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/lineage/lifecycle.py +0 -0
  71. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/lineage/tracker.py +0 -0
  72. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/semantic/__init__.py +0 -0
  73. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/semantic/matcher.py +0 -0
  74. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/semantic/sketch.py +0 -0
  75. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/semantic/thresholds.py +0 -0
  76. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/state/cache.py +0 -0
  77. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/state/context.py +0 -0
  78. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/state/ttl.py +0 -0
  79. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/structure/detector.py +0 -0
  80. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/structure/heuristics.py +0 -0
  81. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/security/types.py +0 -0
  82. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/__init__.py +0 -0
  83. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/config.py +0 -0
  84. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/dispatcher.py +0 -0
  85. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/safe_sender.py +0 -0
  86. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/safe_sender1.py +0 -0
  87. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/token_auth.py +0 -0
  88. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/tracker.py +0 -0
  89. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/telemetry/usage_limits.py +0 -0
  90. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/test_server.py +0 -0
  91. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/__init__.py +0 -0
  92. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/ab_test_controller.py +0 -0
  93. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/auto_retrain_manager.py +0 -0
  94. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/auto_trainer.py +0 -0
  95. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/budget_manager.py +0 -0
  96. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/budget_recommendation.py +0 -0
  97. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/cli_integration.py +0 -0
  98. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/config.py +0 -0
  99. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/daily_pacing_engine.py +0 -0
  100. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/data_extraction.py +0 -0
  101. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/drift_detector.py +0 -0
  102. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/drift_monitor_service.py +0 -0
  103. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/evaluation.py +0 -0
  104. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/feature_engineering.py +0 -0
  105. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/hourly_patterns.py +0 -0
  106. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/local_budget_checker.py +0 -0
  107. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/monitor.py +0 -0
  108. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/performance_tracker.py +0 -0
  109. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/phase2_config.py +0 -0
  110. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/predict.py +0 -0
  111. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/runtime_predictor.py +0 -0
  112. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/session_tracker.py +0 -0
  113. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/soft_limit_injector.py +0 -0
  114. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/spend_forecaster.py +0 -0
  115. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_fetch_budget_api.py +0 -0
  116. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_phase1.py +0 -0
  117. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_phase2.1_complete.py +0 -0
  118. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_phase2.2_live.py +0 -0
  119. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_phase2_mock.py +0 -0
  120. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tests/test_quick.py +0 -0
  121. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/token_governor.py +0 -0
  122. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/tokenization_learner.py +0 -0
  123. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/train/train_model.py +0 -0
  124. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/__init__.py +0 -0
  125. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/charts/__init__.py +0 -0
  126. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/charts/cost_chart.py +0 -0
  127. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/charts/daily_spend_chart.py +0 -0
  128. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/charts/usage_requests_chart.py +0 -0
  129. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/deployment_map_tab.py +0 -0
  130. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/governance_event_log.py +0 -0
  131. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/live_tab.py +0 -0
  132. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/money_burn_tab.py +0 -0
  133. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/settings_header.py +0 -0
  134. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/text_input.py +0 -0
  135. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/toggle.py +0 -0
  136. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/components/usage_volume_tab.py +0 -0
  137. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/launcher.py +0 -0
  138. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/ui/main_window.py +0 -0
  139. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/EXTINCT-model_registry.py +0 -0
  140. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/__init__.py +0 -0
  141. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/budget_sync.py +0 -0
  142. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/csv_logger.py +0 -0
  143. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/helpers.py +0 -0
  144. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/logger.py +0 -0
  145. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/metric_aggregator.py +0 -0
  146. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/run_streamlit_with_shutdown.py +0 -0
  147. {docoreai-2.3.7 → docoreai-2.3.9}/docore_ai/utils/show_app.py +0 -0
  148. {docoreai-2.3.7 → docoreai-2.3.9}/requirements.txt +0 -0
  149. {docoreai-2.3.7 → docoreai-2.3.9}/setup.cfg +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docoreai
3
- Version: 2.3.7
3
+ Version: 2.3.9
4
4
  Summary: Self-tuning AI Cost Governance, Built Privacy-First.
5
5
  Home-page: https://docoreai.com
6
- Author: Saji John Miranda
6
+ Author: Saji John
7
7
  Author-email: saji.john@docoreai.com
8
8
  License: CC BY-NC-ND 4.0
9
9
  Project-URL: Homepage, https://docoreai.com
@@ -53,6 +53,8 @@ Requires-Dist: charset-normalizer>=3.0.0
53
53
  Requires-Dist: google-genai
54
54
  Requires-Dist: prophet>=1.1
55
55
  Requires-Dist: cloud-detect>=0.0.15
56
+ Requires-Dist: sentence-transformers
57
+ Requires-Dist: llmlingua
56
58
  Dynamic: author
57
59
  Dynamic: author-email
58
60
  Dynamic: classifier
@@ -105,7 +107,7 @@ LLM costs do not self-regulate. Without active budget control, a traffic surge a
105
107
 
106
108
  DoCoreAI sits between your application and any LLM provider — OpenAI, Anthropic, Google, Groq, AWS Bedrock — and acts as an autonomous cost and governance layer. It instruments every LLM call your application makes: tracking cost, tokens, latency, and governance decisions — without ever capturing prompt content, response content, or user data.
107
109
 
108
- It learns your actual usage patterns. It predicts what each request genuinely needs. It replaces the wasteful default token ceiling with a precise ML prediction. It paces your budget intelligently across the full day and across the full week, accounting for day-of-week traffic patterns. When its own accuracy degrades, it retrains automatically. Your prompts never leave your network.
110
+ It learns your actual usage patterns. It predicts what each request genuinely needs. It replaces the wasteful default token ceiling with a precise ML prediction. It paces your budget intelligently across the full day and across the full week, accounting for day-of-week traffic patterns. It trims redundant context before requests leave your environment — shrinking token counts without changing what the model sees. When its own accuracy degrades, it retrains automatically. Your prompts never leave your network.
109
111
 
110
112
  **This is not observability.** Observability shows you what happened after the bill arrives. DoCoreAI predicts, paces, and controls spend before overruns occur — autonomously, on every request.
111
113
 
@@ -189,6 +191,8 @@ The default `max_tokens` on most LLM calls is set to 2,000 or higher — a safe
189
191
 
190
192
  At 100,000 daily calls, that single optimisation saves approximately **$174/day — $63,510/year** per deployment. Before pacing, soft limits, or session governance are applied.
191
193
 
194
+ A second lever reduces input token costs. On agentic turns, only the tool definitions the request actually needs are sent — large tool schemas passed on every call are a common source of silent prompt bloat. On multi-step agent sessions, long tool-call results are compressed before re-entering the context window. Both mechanisms are semantic — meaning is preserved, content is not truncated. Combined with output prediction and pacing, the 40–70% range reflects deployments where all three levers are active.
195
+
192
196
  ---
193
197
 
194
198
  ## Quick Start
@@ -312,7 +316,7 @@ Works out of the box with any combination. No provider-specific configuration re
312
316
  - **Groq** — Llama, Mixtral, and all Groq-hosted models
313
317
  - **Ollama** — Local model deployments
314
318
 
315
- **Multi-LLM orchestration:** LangChain, CrewAI (v1.x — LiteLLM-backed), OpenAI Agents SDK, and custom orchestration frameworks are all supported transparently. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call.
319
+ **Multi-LLM orchestration:** LangChain, CrewAI (v1.x — LiteLLM-backed), OpenAI Agents SDK, and custom orchestration frameworks are all supported transparently. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call.
316
320
 
317
321
  ---
318
322
 
@@ -337,6 +341,7 @@ Works out of the box with any combination. No provider-specific configuration re
337
341
  | Executive reporting suite | ✗ | ✓ 7 reports |
338
342
  | Compliance-ready architecture | ✗ | ✓ |
339
343
  | Self-improving prediction model | ✗ | ✓ Auto-retrain + A/B test |
344
+ | Context optimization (tool schema + result compression) | ✗ | ✓ |
340
345
  | Closed source / IP protected | ✗ | ✓ from v2.0 |
341
346
  | SOC2 | Varies | In progress |
342
347
 
@@ -36,7 +36,7 @@ LLM costs do not self-regulate. Without active budget control, a traffic surge a
36
36
 
37
37
  DoCoreAI sits between your application and any LLM provider — OpenAI, Anthropic, Google, Groq, AWS Bedrock — and acts as an autonomous cost and governance layer. It instruments every LLM call your application makes: tracking cost, tokens, latency, and governance decisions — without ever capturing prompt content, response content, or user data.
38
38
 
39
- It learns your actual usage patterns. It predicts what each request genuinely needs. It replaces the wasteful default token ceiling with a precise ML prediction. It paces your budget intelligently across the full day and across the full week, accounting for day-of-week traffic patterns. When its own accuracy degrades, it retrains automatically. Your prompts never leave your network.
39
+ It learns your actual usage patterns. It predicts what each request genuinely needs. It replaces the wasteful default token ceiling with a precise ML prediction. It paces your budget intelligently across the full day and across the full week, accounting for day-of-week traffic patterns. It trims redundant context before requests leave your environment — shrinking token counts without changing what the model sees. When its own accuracy degrades, it retrains automatically. Your prompts never leave your network.
40
40
 
41
41
  **This is not observability.** Observability shows you what happened after the bill arrives. DoCoreAI predicts, paces, and controls spend before overruns occur — autonomously, on every request.
42
42
 
@@ -120,6 +120,8 @@ The default `max_tokens` on most LLM calls is set to 2,000 or higher — a safe
120
120
 
121
121
  At 100,000 daily calls, that single optimisation saves approximately **$174/day — $63,510/year** per deployment. Before pacing, soft limits, or session governance are applied.
122
122
 
123
+ A second lever reduces input token costs. On agentic turns, only the tool definitions the request actually needs are sent — large tool schemas passed on every call are a common source of silent prompt bloat. On multi-step agent sessions, long tool-call results are compressed before re-entering the context window. Both mechanisms are semantic — meaning is preserved, content is not truncated. Combined with output prediction and pacing, the 40–70% range reflects deployments where all three levers are active.
124
+
123
125
  ---
124
126
 
125
127
  ## Quick Start
@@ -243,7 +245,7 @@ Works out of the box with any combination. No provider-specific configuration re
243
245
  - **Groq** — Llama, Mixtral, and all Groq-hosted models
244
246
  - **Ollama** — Local model deployments
245
247
 
246
- **Multi-LLM orchestration:** LangChain, CrewAI (v1.x — LiteLLM-backed), OpenAI Agents SDK, and custom orchestration frameworks are all supported transparently. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call.
248
+ **Multi-LLM orchestration:** LangChain, CrewAI (v1.x — LiteLLM-backed), OpenAI Agents SDK, and custom orchestration frameworks are all supported transparently. DoCoreAI patches at the SDK level — whatever framework sits above it, the governance fires on every call.
247
249
 
248
250
  ---
249
251
 
@@ -268,6 +270,7 @@ Works out of the box with any combination. No provider-specific configuration re
268
270
  | Executive reporting suite | ✗ | ✓ 7 reports |
269
271
  | Compliance-ready architecture | ✗ | ✓ |
270
272
  | Self-improving prediction model | ✗ | ✓ Auto-retrain + A/B test |
273
+ | Context optimization (tool schema + result compression) | ✗ | ✓ |
271
274
  | Closed source / IP protected | ✗ | ✓ from v2.0 |
272
275
  | SOC2 | Varies | In progress |
273
276
 
@@ -208,6 +208,13 @@ def inject_system_prompt(provider: str, kwargs: dict, model_name: str) -> None:
208
208
  # if not active_prompt:
209
209
  # return
210
210
 
211
+ # Skip soft limit injection when tools are present — word-count guidance
212
+ # is meaningless for tool-calling turns (mirrors max_tokens skip logic)
213
+ if kwargs.get('tools'):
214
+ dprint("⏭️ Soft limit injection skipped — tools present in request")
215
+ kwargs['_docoreai_soft_limit_tokens_applied'] = 0
216
+ return
217
+
211
218
  # Get soft limit text — generated dynamically from governance ceiling
212
219
  soft_limit_tokens = (
213
220
  kwargs.get('_docoreai_injected_max_tokens') or
@@ -1086,7 +1093,59 @@ def prepare_and_log_telemetry(args, kwargs, response, provider, in_learning_wind
1086
1093
  dprint(f"⚠️ Response metrics logging failed: {response_log_error}")
1087
1094
 
1088
1095
 
1096
+ # Responses API path: system prompt is in 'instructions' kwarg, not messages.
1097
+ # _docoreai_system_prompt_tokens will be 0 in that case — fall back to instructions.
1098
+ _sys_tokens = kwargs.get('_docoreai_system_prompt_tokens', 0)
1099
+ if not _sys_tokens:
1100
+ _instr = kwargs.get('instructions', '') or ''
1101
+ if _instr:
1102
+ _sys_tokens = len(_instr) // 4
1103
+ dprint(f"📊 system_prompt_tokens from 'instructions' kwarg: {_sys_tokens}")
1089
1104
 
1105
+ # ── Input Savings Calculation ──────────────────────────────────────────
1106
+ # Two separate optimizations reduce LLM INPUT tokens. Each is measured
1107
+ # from values stashed in kwargs during the pre-call phase.
1108
+ #
1109
+ # 1. TOOL PRUNING (tool_pruning_cache.py → check_cache())
1110
+ # Removes entire tool DEFINITIONS from the `tools` parameter.
1111
+ # Values: _docoreai_tools_prompt_length (chars, original definitions)
1112
+ # _docoreai_tools_prompt_length_after_prune (chars, after pruning)
1113
+ # _docoreai_tools_prompt_tokens (token count, original)
1114
+ # Method: byte-ratio on the definition JSON — same content type, accurate.
1115
+ #
1116
+ # 2. PROMPT COMPRESSION (prompt_compression.py → compress_agentic_tool_results())
1117
+ # Reduces role='tool' RESULT MESSAGES in the `messages` parameter.
1118
+ # Values: _docoreai_tool_content_original (chars, original tool results)
1119
+ # _docoreai_tool_content_after_comp (chars, after LLMLingua-2)
1120
+ # Method: fixed 4 chars/token — tool results are plain text, not JSON.
1121
+ # DO NOT use tools_prompt_tokens ratio here: that ratio is for
1122
+ # structured definition JSON (~5–7 chars/tok), not plain text.
1123
+ # ──────────────────────────────────────────────────────────────────────
1124
+ _tel_tpt = kwargs.get('_docoreai_tools_prompt_tokens', 0) or 0
1125
+ _tel_tpl = kwargs.get('_docoreai_tools_prompt_length', 0) or 0
1126
+ _tel_pruned = kwargs.get('_docoreai_tools_prompt_length_after_prune', 0) or 0
1127
+ _tel_tco = kwargs.get('_docoreai_tool_content_original', 0) or 0
1128
+ _tel_tca = kwargs.get('_docoreai_tool_content_after_comp', 0) or 0
1129
+
1130
+ # 1. TOOL PRUNING savings — byte-ratio on tool definition JSON
1131
+ _tok_prune = 0
1132
+ if _tel_tpl > 0 and _tel_pruned < _tel_tpl and _tel_tpt > 0:
1133
+ _tok_prune = max(0, round(_tel_tpt * (1.0 - _tel_pruned / _tel_tpl)))
1134
+
1135
+ # 2. PROMPT COMPRESSION savings — 4 chars/token for plain tool result text
1136
+ _tok_comp = 0
1137
+ if _tel_tco > 0 and _tel_tca < _tel_tco:
1138
+ _tok_comp = max(0, round((_tel_tco - _tel_tca) / 4.0))
1139
+
1140
+ # Combined input tokens saved from both optimizations this call
1141
+ _tokens_saved_input = _tok_prune + _tok_comp
1142
+
1143
+ # Cost saved using price_per_1k_prompt from model_registry at call time.
1144
+ # Stored so historical rows remain accurate if pricing changes later.
1145
+ dprint(f"💰 Input savings — prune: {_tok_prune} tok (tool defs) | "
1146
+ f"comp: {_tok_comp} tok (tool results) | "
1147
+ f"total: {_tokens_saved_input} tok")
1148
+ # ──────────────────────────────────────────────────────────────────────
1090
1149
  payload = prepare_telemetry_payload(
1091
1150
  user_id=state.get("user_id"),
1092
1151
  user_content=user_content,
@@ -1104,13 +1163,18 @@ def prepare_and_log_telemetry(args, kwargs, response, provider, in_learning_wind
1104
1163
  mode='T' if in_learning_window else 'L',
1105
1164
  session_id = kwargs.get('_docoreai_session_id', None),
1106
1165
  session_type = kwargs.get('_docoreai_session_type'),
1107
- system_prompt_tokens = kwargs.get('_docoreai_system_prompt_tokens', 0),
1166
+ system_prompt_tokens = _sys_tokens, #kwargs.get('_docoreai_system_prompt_tokens', 0),
1108
1167
  tools_prompt_tokens = kwargs.get('_docoreai_tools_prompt_tokens', 0),
1109
1168
  tools_prompt_length = kwargs.get('_docoreai_tools_prompt_length', 0),
1110
1169
  temperature = kwargs.get('temperature', -1),
1111
1170
  client_prompt_id = kwargs.get('_docoreai_client_prompt_id'),
1112
1171
  soft_limit_tokens_applied=kwargs.get('_docoreai_soft_limit_tokens_applied',0),
1113
- soft_limit_target_pct_reduced=float(get_phase2_config('soft_limit', 'target_reduction_pct', 0.0))
1172
+ soft_limit_target_pct_reduced=float(get_phase2_config('soft_limit', 'target_reduction_pct', 0.0)),
1173
+ tool_content_original = kwargs.get('_docoreai_tool_content_original', 0),
1174
+ tool_content_after_comp = kwargs.get('_docoreai_tool_content_after_comp', 0),
1175
+ tools_prompt_length_after_prune= kwargs.get('_docoreai_tools_prompt_length_after_prune', 0),
1176
+ tokens_saved_input = _tokens_saved_input, # total input tokens avoided
1177
+
1114
1178
  )
1115
1179
 
1116
1180
  # Attach agentic classification (set by make_llm_wrapper via kwargs stash).
@@ -1122,6 +1186,8 @@ def prepare_and_log_telemetry(args, kwargs, response, provider, in_learning_wind
1122
1186
  payload["call_type"] = _call_type
1123
1187
  payload["framework_name"] = kwargs.get("_docoreai_framework_name") or None
1124
1188
  payload["agent_name"] = kwargs.get("_docoreai_agent_name") or None
1189
+ payload["reasoning_effort"] = kwargs.get("_docoreai_reasoning_effort") or None
1190
+ payload["reasoning_tokens"] = kwargs.get("_docoreai_reasoning_tokens") or None
1125
1191
 
1126
1192
 
1127
1193
  log_telemetry_to_wordpress(success=True, telemetry_data=payload)
@@ -1157,10 +1223,31 @@ def _get_max_tokens_param(model_name: str) -> str | None:
1157
1223
  pass
1158
1224
  return None # not in registry — caller uses 'max_tokens' fallback
1159
1225
  # ------------------------------------------------------------
1226
+ # REASONING MODEL GATE
1227
+ # ------------------------------------------------------------
1228
+ from functools import lru_cache
1229
+
1230
+ @lru_cache(maxsize=128)
1231
+ def _is_reasoning_capable(model_name: str) -> bool:
1232
+ """Return True only for models with reasoning_capable=1 in model_registry."""
1233
+ try:
1234
+ from docore_ai.config.db import get_readonly_connection
1235
+ conn = get_readonly_connection()
1236
+ row = conn.execute(
1237
+ "SELECT reasoning_capable FROM wp_docoreai_model_registry "
1238
+ "WHERE model_name = ? AND status = 'active' LIMIT 1",
1239
+ (model_name,)
1240
+ ).fetchone()
1241
+ conn.close()
1242
+ return bool(row and row[0])
1243
+ except Exception:
1244
+ return False
1245
+
1246
+ # ------------------------------------------------------------
1160
1247
  # UNIVERSAL WRAPPER (provider bound at patch time)
1161
1248
  # + GenAI Runtime Governance (Phase-1)
1162
1249
  # ------------------------------------------------------------
1163
- def make_llm_wrapper(provider, config_provider=None, input_field='messages', output_field='choices[0].message.content'):
1250
+ def make_llm_wrapper(provider, config_provider=None, input_field='messages', output_field='choices[0].message.content', reasoning_extractor=None):
1164
1251
  def llm_wrapper(wrapped, instance, args, kwargs):
1165
1252
  kwargs['_docoreai_input_field'] = input_field
1166
1253
  kwargs['_docoreai_output_field'] = output_field
@@ -1490,7 +1577,7 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
1490
1577
  prompt_tokens = len(full_prompt_text) // 4
1491
1578
  prompt_length = len(full_prompt_text)
1492
1579
  system_prompt_tokens = len(system_prompt_text) // 4
1493
- kwargs['_docoreai_system_prompt_tokens'] = system_prompt_tokens
1580
+
1494
1581
 
1495
1582
  # Count token cost of tool schemas attached to this request
1496
1583
  _tools_in_request = kwargs.get('tools', []) or []
@@ -1499,7 +1586,15 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
1499
1586
  import json as _json_tools
1500
1587
  _tools_json_str = _json_tools.dumps(_tools_in_request)
1501
1588
  tools_prompt_length = len(_tools_json_str)
1502
- tools_prompt_tokens = tools_prompt_length // 4
1589
+ try:
1590
+ from docore_ai.telemetry.token_utils import _resolve_tokenizer
1591
+ _tok_mode, _tok_enc, _ = _resolve_tokenizer(current_model)
1592
+ if _tok_mode == 'tiktoken' and _tok_enc is not None:
1593
+ tools_prompt_tokens = len(_tok_enc.encode(_tools_json_str))
1594
+ else:
1595
+ tools_prompt_tokens = tools_prompt_length // 4
1596
+ except Exception:
1597
+ tools_prompt_tokens = tools_prompt_length // 4
1503
1598
  except Exception:
1504
1599
  tools_prompt_length = 0
1505
1600
  tools_prompt_tokens = 0
@@ -2089,9 +2184,32 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2089
2184
  # (called before STEP 4 so _docoreai_injected_max_tokens not yet set)
2090
2185
  if governance_max_tokens is not None:
2091
2186
  kwargs['_docoreai_governance_max_tokens'] = governance_max_tokens
2092
- # NOW safe to inject system prompt — governance ceiling is stashed
2093
- inject_system_prompt(provider, kwargs, kwargs.get("model") or "unknown")
2187
+ # Skip soft limit injection when tools are present — word-count
2188
+ # guidance is meaningless for tool-calling turns.
2189
+ if not kwargs.get('tools'):
2190
+ inject_system_prompt(provider, kwargs, kwargs.get("model") or "unknown")
2191
+ else:
2192
+ dprint("⏭️ Soft limit injection skipped — tools present in request")
2193
+ kwargs['_docoreai_soft_limit_tokens_applied'] = 0
2094
2194
 
2195
+ # Recompute system_prompt_tokens after injection so the telemetry
2196
+ # captures the injected text that just landed in messages/instructions/system
2197
+ try:
2198
+ _input_field_for_sys = kwargs.get('_docoreai_input_field', 'messages')
2199
+ _msgs_after_inject = kwargs.get(_input_field_for_sys, []) or []
2200
+ _sys_text_after = " ".join([
2201
+ m.get('content', '') for m in _msgs_after_inject
2202
+ if isinstance(m, dict) and m.get('role') == 'system'
2203
+ ])
2204
+ # Anthropic kwarg path
2205
+ _anth_sys = kwargs.get('system', '') or ''
2206
+ if isinstance(_anth_sys, list):
2207
+ _anth_sys = ' '.join(b.get('text', '') for b in _anth_sys if isinstance(b, dict))
2208
+ if _anth_sys:
2209
+ _sys_text_after = _anth_sys + ' ' + _sys_text_after
2210
+ kwargs['_docoreai_system_prompt_tokens'] = len(_sys_text_after) // 4
2211
+ except Exception:
2212
+ pass # non-critical — leave stashed value
2095
2213
  # ============================================================
2096
2214
  # STEP 4: SET max_tokens
2097
2215
  # ============================================================
@@ -2162,8 +2280,15 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2162
2280
  from docore_ai.providers.bedrock_adapter import (
2163
2281
  inject_bedrock_max_tokens,
2164
2282
  )
2165
- inject_bedrock_max_tokens(args, final_max_tokens)
2166
- kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2283
+ _has_tools = bool(kwargs.get('tools') or kwargs.get('functions'))
2284
+ if _has_tools:
2285
+ kwargs['_docoreai_injected_max_tokens'] = None
2286
+ dprint("🔧 Tools present — Bedrock max_tokens injection skipped")
2287
+ if _client_max:
2288
+ dprint(f"⚠️ Client has set max_tokens={_client_max} on a tool-call turn — may truncate tool-call JSON if response exceeds limit")
2289
+ else:
2290
+ inject_bedrock_max_tokens(args, final_max_tokens)
2291
+ kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2167
2292
  except Exception as _binj:
2168
2293
  dprint(f"⚠️ Bedrock max_tokens injection failed: {_binj}")
2169
2294
  else:
@@ -2190,11 +2315,25 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2190
2315
  kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2191
2316
  else:
2192
2317
  _out_field = kwargs.get('_docoreai_output_field', '')
2193
- if _out_field == 'output_text':
2318
+ # ── Tool-call guard ──────────────────────────────────
2319
+ # If tools/functions are present, the model may return
2320
+ # tool-call JSON on this turn. A hard max_tokens cap
2321
+ # can truncate the JSON mid-structure → parse error in
2322
+ # the tool runner → crashed agentic loop.
2323
+ # Soft-limit system prompt (Step 3) is still active and
2324
+ # safe — the model ignores word-count hints for JSON output.
2325
+ _has_tools = bool(kwargs.get('tools') or kwargs.get('functions'))
2326
+ if _has_tools:
2327
+ kwargs['_docoreai_injected_max_tokens'] = None
2328
+ dprint("🔧 Tools present — max_tokens injection skipped to protect tool-call JSON")
2329
+ if _client_max:
2330
+ dprint(f"⚠️ Client has set max_tokens={_client_max} on a tool-call turn — may truncate tool-call JSON if response exceeds limit")
2331
+ elif _out_field == 'output_text':
2194
2332
  kwargs['max_output_tokens'] = int(final_max_tokens)
2333
+ kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2195
2334
  else:
2196
2335
  kwargs[_param] = int(final_max_tokens)
2197
- kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2336
+ kwargs['_docoreai_injected_max_tokens'] = int(final_max_tokens)
2198
2337
  except Exception as _inj_err:
2199
2338
  dprint(f"⚠️ Token ceiling injection failed — call proceeds ungoverned: {_inj_err}")
2200
2339
  except Exception as prediction_error:
@@ -2229,6 +2368,9 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2229
2368
  final_kwargs.pop("_docoreai_system_prompt_tokens", None)
2230
2369
  final_kwargs.pop("_docoreai_tools_prompt_tokens", None)
2231
2370
  final_kwargs.pop("_docoreai_tools_prompt_length", None)
2371
+ final_kwargs.pop("_docoreai_tool_content_original", None)
2372
+ final_kwargs.pop("_docoreai_tool_content_after_comp", None)
2373
+ final_kwargs.pop("_docoreai_tools_prompt_length_after_prune", None)
2232
2374
  final_kwargs.pop("_docoreai_client_max_tokens", None)
2233
2375
  final_kwargs.pop('_docoreai_injected_max_tokens', None)
2234
2376
  final_kwargs.pop('_docoreai_governance_max_tokens', None)
@@ -2275,7 +2417,77 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2275
2417
  else:
2276
2418
  dprint("🔧 No tools in final API request")
2277
2419
 
2420
+ # ── Prompt Compression — agentic tool results in live mode only────────────────
2421
+ kwargs['_docoreai_tool_content_original'] = 0
2422
+ kwargs['_docoreai_tool_content_after_comp'] = 0
2423
+ kwargs['_docoreai_tools_prompt_length_after_prune'] = kwargs.get('_docoreai_tools_prompt_length', 0)
2424
+ _original_tools = final_kwargs.get('tools')
2425
+ _pruned = None # Track HIT/MISS — guards post-call update_cache
2426
+
2427
+ if not in_learning_window:
2428
+ # ── Prompt Compression — agentic tool results ────────────
2429
+ if final_kwargs.get('messages') and final_kwargs.get('tools'):
2430
+ try:
2431
+ from docore_ai.instrumentation.agentic_cost.prompt_compression import compress_agentic_tool_results
2432
+ # Gate: skip log noise entirely when compression is disabled
2433
+ try:
2434
+ from docore_ai.config.db import get_phase2_config as _gpc_pc
2435
+ _pc_enabled = _gpc_pc('prompt_compression', 'enabled', False)
2436
+ except Exception:
2437
+ _pc_enabled = False
2438
+
2439
+ if _pc_enabled:
2440
+ _msgs_before = final_kwargs['messages']
2441
+ _chars_before = sum(len(str(m.get('content', ''))) for m in _msgs_before if isinstance(m.get('content'), str))
2442
+ dprint(f"🗜️ [Compression] Before: {_chars_before} chars across {len(_msgs_before)} messages")
2443
+
2444
+ final_kwargs['messages'], _pc_stats = compress_agentic_tool_results(final_kwargs['messages'])
2445
+ _chars_after = _pc_stats.get('chars_after', 0)
2446
+ _chars_orig = _pc_stats.get('chars_original', 0)
2447
+ dprint(f"🗜️ [Compression] After: {_chars_after} chars (saved {_chars_orig - _chars_after}, ratio {round(_chars_after/_chars_orig, 3) if _chars_orig else 'N/A'})")
2448
+
2449
+ kwargs['_docoreai_tool_content_original'] = _pc_stats.get('chars_original', 0)
2450
+ kwargs['_docoreai_tool_content_after_comp'] = _pc_stats.get('chars_after', 0)
2451
+ except Exception as _pc_err:
2452
+ dprint(f"⚠️ Prompt compression failed (non-fatal): {_pc_err}")
2453
+
2454
+ # ── Tool Pruning Cache — check before LLM call ──────────
2455
+ if _original_tools:
2456
+ dprint(f"✂️ [Pruning] Before: {len(_original_tools)} tools, {len(_json_before := __import__('json').dumps(_original_tools))} bytes")
2457
+ try:
2458
+ from docore_ai.instrumentation.agentic_cost.tool_pruning_cache import check_cache
2459
+ _pruned = check_cache(system_prompt_text, prompt_text, _original_tools, actual_provider)
2460
+ if _pruned is not None:
2461
+ final_kwargs['tools'] = _pruned
2462
+ import json as _json_prune
2463
+ _pruned_json_str = _json_prune.dumps(_pruned)
2464
+ kwargs['_docoreai_tools_prompt_length_after_prune'] = len(_pruned_json_str)
2465
+ # Update token count to reflect pruned set (mirrors Fix 1 logic)
2466
+ try:
2467
+ from docore_ai.telemetry.token_utils import _resolve_tokenizer
2468
+ _tok_mode, _tok_enc, _ = _resolve_tokenizer(current_model)
2469
+ if _tok_mode == 'tiktoken' and _tok_enc is not None:
2470
+ kwargs['_docoreai_tools_prompt_tokens'] = len(_tok_enc.encode(_pruned_json_str))
2471
+ else:
2472
+ kwargs['_docoreai_tools_prompt_tokens'] = len(_pruned_json_str) // 4
2473
+ except Exception:
2474
+ kwargs['_docoreai_tools_prompt_tokens'] = len(_pruned_json_str) // 4
2475
+ dprint(f"✂️ [Pruning] After: {len(_pruned)} tools, {kwargs['_docoreai_tools_prompt_length_after_prune']} bytes (removed {len(_original_tools) - len(_pruned)} tools)")
2476
+ else:
2477
+ dprint(f"✂️ [Pruning] Cache miss — all {len(_original_tools)} tools kept")
2478
+ except Exception as _tpc_err:
2479
+ dprint(f"⚠️ Tool pruning cache check failed (non-fatal): {_tpc_err}")
2480
+ else:
2481
+ dprint("📚 Training mode: prompt compression and tool pruning skipped")
2482
+
2278
2483
  response = wrapped(*args, **final_kwargs)
2484
+ # ── Tool Pruning Cache — update after LLM response (Live only) ──
2485
+ if not in_learning_window and _original_tools and _pruned is None:
2486
+ try:
2487
+ from docore_ai.instrumentation.agentic_cost.tool_pruning_cache import update_cache
2488
+ update_cache(system_prompt_text, prompt_text, _original_tools, response, actual_provider)
2489
+ except Exception as _tpu_err:
2490
+ dprint(f"⚠️ Tool pruning cache update failed (non-fatal): {_tpu_err}")
2279
2491
  dprint(f"🔍 Response type: {type(response).__name__} | has_usage={hasattr(response, 'usage')} | stream_in_kwargs={final_kwargs.get('stream', False)}")
2280
2492
  # LangChain/OpenAI tool-calling path may return LegacyAPIResponse.
2281
2493
  # Normalize it before DoCoreAI reads usage/text.
@@ -2443,6 +2655,30 @@ def make_llm_wrapper(provider, config_provider=None, input_field='messages', out
2443
2655
  #response = wrapped(*args, **final_kwargs)
2444
2656
  #kwargs["execution_time"] = round(time.time() - start, 2)
2445
2657
 
2658
+ # ====================================================
2659
+ # 🧠 REASONING CAPTURE (reasoning-capable models only)
2660
+ # ====================================================
2661
+ try:
2662
+ _reasoning_model = kwargs.get('model') or current_model
2663
+ dprint(f"🧠 [reasoning gate] model={_reasoning_model} extractor={reasoning_extractor} capable={_is_reasoning_capable(_reasoning_model)}")
2664
+
2665
+ if reasoning_extractor and _is_reasoning_capable(_reasoning_model):
2666
+ from docore_ai.reasoning_capture import extract_reasoning
2667
+ _reasoning_result = extract_reasoning(
2668
+ reasoning_extractor, kwargs, _telemetry_response
2669
+ )
2670
+ dprint(f"🧠 [reasoning result] {_reasoning_result}")
2671
+ kwargs['_docoreai_reasoning_effort'] = _reasoning_result.get('effort')
2672
+ kwargs['_docoreai_reasoning_tokens'] = _reasoning_result.get('reasoning_tokens')
2673
+ else:
2674
+ dprint(f"🧠 [reasoning skip] extractor={reasoning_extractor} capable={_is_reasoning_capable(_reasoning_model)}")
2675
+ kwargs['_docoreai_reasoning_effort'] = None
2676
+ kwargs['_docoreai_reasoning_tokens'] = None
2677
+ except Exception as _re:
2678
+ dprint(f"⚠️ Reasoning capture failed (non-fatal): {_re}")
2679
+ kwargs['_docoreai_reasoning_effort'] = None
2680
+ kwargs['_docoreai_reasoning_tokens'] = None
2681
+
2446
2682
  # ====================================================
2447
2683
  # 🛡️ GenAI Runtime Governance – REGISTER FINGERPRINTS
2448
2684
  # ====================================================
@@ -2696,6 +2932,10 @@ def log_failed_telemetry(args, kwargs, provider, error, in_learning_window=True)
2696
2932
  client_prompt_id=kwargs.get('_docoreai_client_prompt_id'),
2697
2933
  soft_limit_tokens_applied=kwargs.get('_docoreai_soft_limit_tokens_applied',0),
2698
2934
  soft_limit_target_pct_reduced=float(get_phase2_config('soft_limit', 'target_reduction_pct', 0.0)),
2935
+ tool_content_original = kwargs.get('_docoreai_tool_content_original', 0),
2936
+ tool_content_after_comp = kwargs.get('_docoreai_tool_content_after_comp', 0),
2937
+ tools_prompt_length_after_prune= kwargs.get('_docoreai_tools_prompt_length_after_prune', 0),
2938
+
2699
2939
  )
2700
2940
 
2701
2941
  # ✅ Add error details to payload
@@ -2783,7 +3023,8 @@ def _load_patch_targets() -> list:
2783
3023
  "provider, "
2784
3024
  "response_format, "
2785
3025
  "COALESCE(input_field, 'messages'), "
2786
- "COALESCE(output_field, 'choices[0].message.content') "
3026
+ "COALESCE(output_field, 'choices[0].message.content'), "
3027
+ "reasoning_extractor "
2787
3028
  "FROM wp_docoreai_patch_targets "
2788
3029
  "WHERE enabled = 1 "
2789
3030
  "ORDER BY id ASC"
@@ -2799,7 +3040,8 @@ def _load_patch_targets() -> list:
2799
3040
  r[2],
2800
3041
  r[3],
2801
3042
  r[4] if len(r) > 4 else 'messages',
2802
- r[5] if len(r) > 5 else 'choices[0].message.content'
3043
+ r[5] if len(r) > 5 else 'choices[0].message.content',
3044
+ r[6] if len(r) > 6 else None
2803
3045
  )
2804
3046
  for r in rows
2805
3047
  ]
@@ -2891,7 +3133,11 @@ def apply_patches():
2891
3133
 
2892
3134
  for target in active_targets:
2893
3135
 
2894
- # 6-tuple from DB:
3136
+ # 7-tuple from DB:
3137
+ # (module_path, class_method, provider,
3138
+ # response_format, input_field, output_field, reasoning_extractor)
3139
+ #
3140
+ # 6-tuple from DB (legacy):
2895
3141
  # (module_path, class_method, provider,
2896
3142
  # response_format, input_field, output_field)
2897
3143
  #
@@ -2901,7 +3147,20 @@ def apply_patches():
2901
3147
  # 3-tuple compatibility:
2902
3148
  # (module_path, class_method, provider)
2903
3149
 
2904
- if len(target) == 6:
3150
+ reasoning_extractor = None # default
3151
+
3152
+ if len(target) == 7:
3153
+ (
3154
+ module_path,
3155
+ attr_path,
3156
+ provider,
3157
+ response_format,
3158
+ input_field,
3159
+ output_field,
3160
+ reasoning_extractor
3161
+ ) = target
3162
+
3163
+ elif len(target) == 6:
2905
3164
  (
2906
3165
  module_path,
2907
3166
  attr_path,
@@ -2961,7 +3220,8 @@ def apply_patches():
2961
3220
  make_llm_wrapper(
2962
3221
  provider,
2963
3222
  input_field=input_field,
2964
- output_field=output_field
3223
+ output_field=output_field,
3224
+ reasoning_extractor=reasoning_extractor
2965
3225
  )
2966
3226
  )
2967
3227
 
@@ -281,6 +281,7 @@ def start():
281
281
 
282
282
  if not _check_membership():
283
283
  dprint("🛑 DoCoreAI cannot start — membership invalid.")
284
+ print("🛑 DoCoreAI stopped — membership invalid. Check the log file for details.", flush=True)
284
285
  return
285
286
 
286
287
  # Start auto-retraining monitor (Phase 2.2)
@@ -386,7 +387,37 @@ def start():
386
387
  # dprint("📚 Learning phase active - budget not required yet")
387
388
  # dprint(f" {get_prediction_count()}/100 predictions collected")
388
389
  #dprint("✅ All systems ready")
389
- show_start_banner()
390
+ # Pre-warm tool pruning embedder (downloads model once, cached forever after)
391
+ try:
392
+ if get_phase2_config('tool_pruning', 'enabled', True):
393
+ from docore_ai.instrumentation.agentic_cost.embedder import encode
394
+ encode('')
395
+ dprint("✅ Tool pruning embedder pre-loaded")
396
+ except Exception as e:
397
+ dprint(f"⚠️ Tool pruning embedder warm-up skipped: {e}")
398
+
399
+ # Pre-warm LLMLingua-2 prompt compressor in a background thread
400
+ # Model cold load takes 60–190 s — must NOT block the start command
401
+ try:
402
+ _pc_en = str(get_phase2_config('prompt_compression', 'enabled', 'false')).lower() == 'true'
403
+ _pc_ag = str(get_phase2_config('prompt_compression', 'agentic_enabled', 'false')).lower() == 'true'
404
+ _pc_rt = get_phase2_config('prompt_compression', 'agentic_rate', '0.9')
405
+ _pc_status = "ENABLED ✅" if (_pc_en and _pc_ag) else "DISABLED ⛔"
406
+ dprint(f"🗜️ Prompt compression: {_pc_status} | enabled={_pc_en}, agentic={_pc_ag}, rate={_pc_rt}")
407
+ if _pc_en and _pc_ag:
408
+ import threading as _threading
409
+ def _warm_compressor():
410
+ try:
411
+ from docore_ai.instrumentation.agentic_cost.prompt_compression import preload_compressor
412
+ preload_compressor()
413
+ except Exception as _wc_err:
414
+ dprint(f"⚠️ Prompt compressor warm-up failed: {_wc_err}")
415
+ _t = _threading.Thread(target=_warm_compressor, daemon=True, name="compressor-preload")
416
+ _t.start()
417
+ dprint("🗜️ Prompt compressor pre-load started (background thread)")
418
+ except Exception as e:
419
+ dprint(f"⚠️ Prompt compressor warm-up skipped: {e}")
420
+ show_start_banner()
390
421
 
391
422
 
392
423
  # 🔴 FINAL STEP: Trigger autopatch via .pth
@@ -1016,6 +1047,7 @@ def _check_membership() -> bool:
1016
1047
 
1017
1048
  if state == "expired":
1018
1049
  dprint(f"❌ Membership expired — please renew at docoreai.com")
1050
+ print("❌ DoCoreAI cannot start — membership expired. Please renew at docoreai.com", flush=True)
1019
1051
  return False
1020
1052
  # unknown/other states — allow through (endpoint may be returning partial data)
1021
1053
  if state != "active":