Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/crewai/src/crewai/events/types/llm_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class LLMCallCompletedEvent(LLMEventBase):
messages: str | list[dict[str, Any]] | None = None
response: Any
call_type: LLMCallType
stop_reason: str | None = None


class LLMCallFailedEvent(LLMEventBase):
Expand Down
2 changes: 2 additions & 0 deletions lib/crewai/src/crewai/llms/base_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def _emit_call_completed_event(
from_task: Task | None = None,
from_agent: Agent | None = None,
messages: str | list[LLMMessage] | None = None,
stop_reason: str | None = None,
) -> None:
"""Emit LLM call completed event."""
from crewai.utilities.serialization import to_serializable
Expand All @@ -426,6 +427,7 @@ def _emit_call_completed_event(
from_agent=from_agent,
model=self.model,
call_id=get_current_call_id(),
stop_reason=stop_reason,
),
)

Expand Down
54 changes: 54 additions & 0 deletions lib/crewai/src/crewai/llms/providers/anthropic/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,26 @@ def _convert_image_blocks(content: Any) -> Any:

return converted

@staticmethod
def _extract_stop_reason(response: Message | BetaMessage) -> str | None:
"""Extract stop_reason from an Anthropic response, returning None if absent."""
raw = getattr(response, "stop_reason", None)
return raw if isinstance(raw, str) else None

def _warn_if_truncated(
self,
response: Message | BetaMessage,
from_agent: Any | None = None,
) -> None:
"""Log a warning if the response was truncated due to max_tokens."""
stop_reason = self._extract_stop_reason(response)
if stop_reason == "max_tokens":
agent_hint = f" [{from_agent.role}]" if from_agent else ""
logging.warning(
f"Truncated response{agent_hint}: stop_reason='max_tokens'. "
f"Consider increasing max_tokens (current: {self.max_tokens})."
)

def _format_messages_for_anthropic(
self, messages: str | list[LLMMessage]
) -> tuple[list[LLMMessage], str | None]:
Expand Down Expand Up @@ -858,6 +878,9 @@ def _handle_completion(
usage = self._extract_anthropic_token_usage(response)
self._track_token_usage_internal(usage)

stop_reason = self._extract_stop_reason(response)
self._warn_if_truncated(response, from_agent)

if _is_pydantic_model_class(response_model) and response.content:
if use_native_structured_output:
for block in response.content:
Expand All @@ -869,6 +892,7 @@ def _handle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data
else:
Expand All @@ -884,6 +908,7 @@ def _handle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data

Expand All @@ -906,6 +931,7 @@ def _handle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return list(tool_uses)

Expand Down Expand Up @@ -937,6 +963,7 @@ def _handle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)

if usage.get("total_tokens", 0) > 0:
Expand Down Expand Up @@ -1077,6 +1104,9 @@ def _handle_streaming_completion(
usage = self._extract_anthropic_token_usage(final_message)
self._track_token_usage_internal(usage)

stop_reason = self._extract_stop_reason(final_message)
self._warn_if_truncated(final_message, from_agent)

if _is_pydantic_model_class(response_model):
if use_native_structured_output:
structured_data = response_model.model_validate_json(full_response)
Expand All @@ -1086,6 +1116,7 @@ def _handle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data
for block in final_message.content:
Expand All @@ -1100,6 +1131,7 @@ def _handle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data

Expand Down Expand Up @@ -1129,6 +1161,7 @@ def _handle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)

return self._invoke_after_llm_call_hooks(
Expand Down Expand Up @@ -1275,6 +1308,9 @@ def _handle_tool_use_conversation(
follow_up_usage = self._extract_anthropic_token_usage(final_response)
self._track_token_usage_internal(follow_up_usage)

stop_reason = self._extract_stop_reason(final_response)
self._warn_if_truncated(final_response, from_agent)

final_content = ""
thinking_blocks: list[ThinkingBlock] = []

Expand All @@ -1299,6 +1335,7 @@ def _handle_tool_use_conversation(
from_task=from_task,
from_agent=from_agent,
messages=follow_up_params["messages"],
stop_reason=stop_reason,
)

# Log combined token usage
Expand Down Expand Up @@ -1379,6 +1416,9 @@ async def _ahandle_completion(
usage = self._extract_anthropic_token_usage(response)
self._track_token_usage_internal(usage)

stop_reason = self._extract_stop_reason(response)
self._warn_if_truncated(response, from_agent)

if _is_pydantic_model_class(response_model) and response.content:
if use_native_structured_output:
for block in response.content:
Expand All @@ -1390,6 +1430,7 @@ async def _ahandle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data
else:
Expand All @@ -1405,6 +1446,7 @@ async def _ahandle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data

Expand All @@ -1425,6 +1467,7 @@ async def _ahandle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return list(tool_uses)

Expand All @@ -1448,6 +1491,7 @@ async def _ahandle_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)

if usage.get("total_tokens", 0) > 0:
Expand Down Expand Up @@ -1576,6 +1620,9 @@ async def _ahandle_streaming_completion(
usage = self._extract_anthropic_token_usage(final_message)
self._track_token_usage_internal(usage)

stop_reason = self._extract_stop_reason(final_message)
self._warn_if_truncated(final_message, from_agent)

if _is_pydantic_model_class(response_model):
if use_native_structured_output:
structured_data = response_model.model_validate_json(full_response)
Expand All @@ -1585,6 +1632,7 @@ async def _ahandle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data
for block in final_message.content:
Expand All @@ -1599,6 +1647,7 @@ async def _ahandle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)
return structured_data

Expand Down Expand Up @@ -1627,6 +1676,7 @@ async def _ahandle_streaming_completion(
from_task=from_task,
from_agent=from_agent,
messages=params["messages"],
stop_reason=stop_reason,
)

return full_response
Expand Down Expand Up @@ -1671,6 +1721,9 @@ async def _ahandle_tool_use_conversation(
follow_up_usage = self._extract_anthropic_token_usage(final_response)
self._track_token_usage_internal(follow_up_usage)

stop_reason = self._extract_stop_reason(final_response)
self._warn_if_truncated(final_response, from_agent)

final_content = ""
if final_response.content:
for content_block in final_response.content:
Expand All @@ -1685,6 +1738,7 @@ async def _ahandle_tool_use_conversation(
from_task=from_task,
from_agent=from_agent,
messages=follow_up_params["messages"],
stop_reason=stop_reason,
)

total_usage = {
Expand Down
Loading
Loading