Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(agent): centralize runtime defaults in llm_defaults.json
trohitg9c26d92f2d71
diff --git a/server/config/llm_defaults.json b/server/config/llm_defaults.jsonindex ebc41516..76a76dea 100644--- a/server/config/llm_defaults.json+++ b/server/config/llm_defaults.json@@ -3,6 +3,15 @@"last_updated": "2026-04-03","_description": "LLM provider defaults. Connection config (base_url, models_endpoint) drives both native SDK and LangChain paths. Model specs auto-enriched by model_registry.json (OpenRouter cache).",+ "agent": {+ "_description": "Agent execution defaults shared across the LangGraph + compaction layers.",+ "recursion_limit": 50,+ "default_temperature": 0.7,+ "compaction": {+ "ratio": 0.5+ }+ },+"providers": {"openai": {"default_model": "gpt-5.4",diff --git a/server/nodes/agent/_specialized.py b/server/nodes/agent/_specialized.pyindex fc8fe663..e499fff5 100644--- a/server/nodes/agent/_specialized.py+++ b/server/nodes/agent/_specialized.py@@ -48,12 +48,17 @@ class SpecializedAgentParams(BaseModel):)# ---- "Options" group (collapsed by default in the parameter panel) ----- temperature: float = Field(- default=0.7, ge=0.0, le=2.0,+ # default=None so an unset value falls through to ``agent.default_temperature``+ # in server/config/llm_defaults.json (resolved by _resolve_temperature).+ temperature: Optional[float] = Field(+ default=None, ge=0.0, le=2.0,json_schema_extra={"group": "options"},)+ # default=None so an unset value is absent from the dumped dict and the+ # backend (_resolve_max_tokens) falls through to the per-model default+ # in server/config/llm_defaults.json instead of being silently capped.max_tokens: Optional[int] = Field(- default=1000, ge=1, le=200000,+ default=None, ge=1, le=200000,json_schema_extra={"group": "options"},)diff --git a/server/nodes/agent/ai_agent.py b/server/nodes/agent/ai_agent.pyindex b080fc47..c5e3724a 100644--- a/server/nodes/agent/ai_agent.py+++ b/server/nodes/agent/ai_agent.py@@ -51,12 +51,17 @@ class AIAgentParams(BaseModel):)# ---- "Options" group (collapsed by default in the parameter panel) ----- temperature: float = Field(- default=0.7, ge=0.0, le=2.0,+ # default=None so an unset value falls through to ``agent.default_temperature``+ # in server/config/llm_defaults.json (resolved by _resolve_temperature).+ temperature: Optional[float] = Field(+ default=None, ge=0.0, le=2.0,json_schema_extra={"numberStepSize": 0.1, "group": "options"},)+ # default=None so an unset value is absent from the dumped dict and the+ # backend (_resolve_max_tokens) falls through to the per-model default+ # in server/config/llm_defaults.json instead of being silently capped.max_tokens: Optional[int] = Field(- default=1000, ge=1, le=200000,+ default=None, ge=1, le=200000,json_schema_extra={"group": "options"},)diff --git a/server/nodes/agent/chat_agent.py b/server/nodes/agent/chat_agent.pyindex 1e58e8ce..f8295744 100644--- a/server/nodes/agent/chat_agent.py+++ b/server/nodes/agent/chat_agent.py@@ -44,12 +44,17 @@ class ChatAgentParams(BaseModel):)# ---- "Options" group (collapsed by default in the parameter panel) ----- temperature: float = Field(- default=0.7, ge=0.0, le=2.0,+ # default=None so an unset value falls through to ``agent.default_temperature``+ # in server/config/llm_defaults.json (resolved by _resolve_temperature).+ temperature: Optional[float] = Field(+ default=None, ge=0.0, le=2.0,json_schema_extra={"group": "options"},)+ # default=None so an unset value is absent from the dumped dict and the+ # backend (_resolve_max_tokens) falls through to the per-model default+ # in server/config/llm_defaults.json instead of being silently capped.max_tokens: Optional[int] = Field(- default=1000, ge=1, le=200000,+ default=None, ge=1, le=200000,json_schema_extra={"group": "options"},)diff --git a/server/nodes/model/_base.py b/server/nodes/model/_base.pyindex 5195e283..bd4aa099 100644--- a/server/nodes/model/_base.py+++ b/server/nodes/model/_base.py@@ -39,11 +39,16 @@ class ChatModelParams(BaseModel):default="",json_schema_extra={"placeholder": "Select a model..."},)- temperature: float = Field(- default=0.7, ge=0.0, le=2.0,+ # default=None so an unset value falls through to ``agent.default_temperature``+ # in server/config/llm_defaults.json (resolved by _resolve_temperature).+ temperature: Optional[float] = Field(+ default=None, ge=0.0, le=2.0,json_schema_extra={"numberStepSize": 0.1},)- max_tokens: Optional[int] = Field(default=1000, ge=1, le=200000)+ # default=None so an unset value is absent from the dumped dict and the+ # backend (_resolve_max_tokens) falls through to the per-model default+ # in server/config/llm_defaults.json instead of being silently capped.+ max_tokens: Optional[int] = Field(default=None, ge=1, le=200000)system_prompt: Optional[str] = Field(default="",json_schema_extra={"rows": 3, "placeholder": "You are a helpful assistant."},diff --git a/server/services/ai.py b/server/services/ai.pyindex a59df210..3e1c934f 100644--- a/server/services/ai.py+++ b/server/services/ai.py@@ -44,6 +44,7 @@ from langchain_core.messages import HumanMessage, SystemMessage, AIMessage, Base_ailog("importing langgraph + pydantic...")from langchain_core.tools import StructuredToolfrom langgraph.graph import StateGraph, END+from langgraph.errors import GraphRecursionErrorfrom pydantic import BaseModel, Field, create_model_ailog("eager LangChain imports done")import json@@ -375,7 +376,10 @@ def _resolve_temperature(flattened: dict, model: str, provider: str, thinking_enfrom services.model_registry import get_model_registryregistry = get_model_registry()- user_temp = float(flattened.get('temperature', 0.7))+ user_val = flattened.get('temperature')+ if user_val is None:+ user_val = registry.get_agent_defaults()["default_temperature"]+ user_temp = float(user_val)# O-series reasoning models always require temperature=1if registry.is_reasoning_model(model, provider):@@ -847,14 +851,18 @@ def create_tool_node(tool_executor: Callable):def should_continue(state: AgentState) -> str:- """Determine if the agent should continue or end.-- This is the conditional edge function for LangGraph.- Returns "tools" to execute pending tool calls, or "end" to finish.+ """Conditional edge: route to ``tools`` when the LLM emitted tool calls,+ otherwise terminate the graph.++ The previous implementation tracked a custom ``iteration`` counter to cap+ the loop. That counter shared a name with LangGraph internals and was+ being mutated unexpectedly, causing the edge to return ``"end"`` on the+ first tool call. The standard LangGraph pattern is to route purely on+ pending tool calls and rely on ``recursion_limit`` (passed via+ ``ainvoke(config=...)``) as the safety net."""- if state.get("should_continue", False):- if state.get("iteration", 0) < state.get("max_iterations", 10):- return "tools"+ if state.get("pending_tool_calls"):+ return "tools"return "end"@@ -1325,11 +1333,14 @@ class AIService:from services.model_registry import get_model_registryregistry = get_model_registry()- # Force temperature=1 for reasoning models (regardless of thinking mode)+ # Reasoning models (listed in ``reasoning_models`` per provider in+ # llm_defaults.json) do not support the ``temperature`` parameter —+ # the API rejects requests that send any value, including 1. Drop+ # it entirely rather than pass an explicit value.if registry.is_reasoning_model(model, provider):- if kwargs.get('temperature', 1) != 1:- logger.info(f"[AI] Reasoning model '{model}': forcing temperature to 1 (was {kwargs.get('temperature')})")- kwargs['temperature'] = 1+ if 'temperature' in kwargs:+ logger.info(f"[AI] Reasoning model '{model}': omitting temperature param")+ kwargs.pop('temperature', None)if thinking and thinking.enabled:thinking_type = registry.get_thinking_type(model, provider)@@ -1907,9 +1918,29 @@ class AIService:"history_count": history_count})- # Execute the graph using ainvoke for proper async support- # This allows async tool nodes and WebSocket broadcasts to work correctly- final_state = await agent_graph.ainvoke(initial_state)+ # Execute the graph. ``recursion_limit`` (cap on agent/tool+ # supersteps) is sourced from llm_defaults.json so deployments+ # tune it without code changes. On hit, surface what the agent+ # produced before stopping rather than the raw library error.+ from services.model_registry import get_model_registry+ recursion_limit = int(get_model_registry().get_agent_defaults()["recursion_limit"])+ final_state = None+ try:+ async for snapshot in agent_graph.astream(+ initial_state,+ config={"recursion_limit": recursion_limit},+ stream_mode="values",+ ):+ final_state = snapshot+ except GraphRecursionError:+ msgs = (final_state or {}).get("messages") or []+ raise ValueError(+ f"Agent reached the {recursion_limit}-superstep recursion "+ f"limit without producing a final answer "+ f"({len(msgs)} message(s) exchanged). "+ f"Adjust agent.recursion_limit in llm_defaults.json or "+ f"simplify the task."+ )# Extract the AI response (last message in the accumulated messages)all_messages = final_state["messages"]@@ -2345,8 +2376,29 @@ class AIService:"thinking_content": None}- # Execute the graph- final_state = await agent_graph.ainvoke(initial_state)+ # Execute the graph. ``recursion_limit`` is sourced from+ # llm_defaults.json; see ``execute_agent`` for the rationale.+ from services.model_registry import get_model_registry+ recursion_limit = int(+ get_model_registry().get_agent_defaults()["recursion_limit"]+ )+ final_state = None+ try:+ async for snapshot in agent_graph.astream(+ initial_state,+ config={"recursion_limit": recursion_limit},+ stream_mode="values",+ ):+ final_state = snapshot+ except GraphRecursionError:+ msgs = (final_state or {}).get("messages") or []+ raise ValueError(+ f"Agent reached the {recursion_limit}-superstep "+ f"recursion limit without producing a final answer "+ f"({len(msgs)} message(s) exchanged). "+ f"Adjust agent.recursion_limit in llm_defaults.json "+ f"or simplify the task."+ )# Extract responseall_messages = final_state["messages"]diff --git a/server/services/compaction.py b/server/services/compaction.pyindex 10f9eb55..30f293f4 100644--- a/server/services/compaction.py+++ b/server/services/compaction.py@@ -21,9 +21,9 @@ if TYPE_CHECKING:logger = get_logger(__name__)-# Default fraction of context window used as compaction threshold-# Overridden by user_settings.compaction_ratio if configured.-DEFAULT_CONTEXT_THRESHOLD_RATIO = 0.5+# Compaction ratio is sourced from server/config/llm_defaults.json+# (``agent.compaction.ratio``). Per-user override in ``user_settings`` still+# wins via :meth:`CompactionService._get_compaction_ratio`.def _extract_text_from_response(content) -> str:@@ -77,7 +77,9 @@ class CompactionService:self._ai_service = ai_serviceasync def _get_compaction_ratio(self) -> float:- """Get compaction ratio from user settings, falling back to default."""+ """Per-user override from ``user_settings.compaction_ratio``, falling+ back to ``agent.compaction.ratio`` in ``llm_defaults.json``."""+ from services.model_registry import get_model_registrytry:settings = await self._db.get_user_settings("default")if settings and "compaction_ratio" in settings:@@ -86,17 +88,20 @@ class CompactionService:return ratioexcept Exception:pass- return DEFAULT_CONTEXT_THRESHOLD_RATIO+ return float(get_model_registry().get_agent_defaults()["compaction"]["ratio"])- def get_model_threshold(self, model: str, provider: str, ratio: float = DEFAULT_CONTEXT_THRESHOLD_RATIO) -> int:+ def get_model_threshold(self, model: str, provider: str, ratio: Optional[float] = None) -> int:"""Compute compaction threshold based on model's context window.- Returns ratio * model's context length, floored to the global- default if the registry has no data. Per-session custom_threshold- (checked in track()) still takes priority over this value.+ ``ratio`` defaults to ``agent.compaction.ratio`` from+ ``llm_defaults.json``; pass an explicit value to override per call.+ Per-session ``custom_threshold`` (checked in :meth:`track`) still+ wins over the value returned here."""from services.model_registry import get_model_registryregistry = get_model_registry()+ if ratio is None:+ ratio = float(registry.get_agent_defaults()["compaction"]["ratio"])context_length = registry.get_context_length(model, provider)model_threshold = int(context_length * ratio)# Never go below the configured minimum (ge=10000 in Settings)diff --git a/server/services/model_registry.py b/server/services/model_registry.pyindex 38304c71..c0a2c1da 100644--- a/server/services/model_registry.py+++ b/server/services/model_registry.py@@ -483,6 +483,16 @@ class ModelRegistryService:logger.warning(f"Could not load llm_defaults.json: {e}")self._llm_defaults = {"providers": {}}+ def get_agent_defaults(self) -> Dict[str, Any]:+ """Return the ``agent`` block from ``llm_defaults.json``.++ Holds runtime defaults shared across the LangGraph executor and+ compaction layer (e.g. ``recursion_limit``, ``compaction.ratio``).+ Callers should ``.get(...)`` with their own fallback so the system+ still boots if the JSON is missing or partial.+ """+ return self._llm_defaults.get("agent", {})+def _get_default_max_output_tokens(self, provider: str, model: str) -> int:"""Fallback: get max output tokens from llm_defaults.json."""providers = self._llm_defaults.get("providers", {})