karenina.adapters.claude_agent_sdk.llm¶
llm
¶
Claude Agent SDK LLM adapter implementing the LLMPort interface.
This module provides the ClaudeSDKLLMAdapter class that implements the LLMPort interface using the Claude Agent SDK's query() function for simple LLM calls.
IMPORTANT: Uses query() NOT ClaudeSDKClient since no hooks/tools are needed for simple LLM invocation. For agent loops with MCP/hooks, use ClaudeSDKAgentAdapter.
Key differences from LangChain: - Claude SDK uses string prompts, not message arrays - System prompts go in ClaudeAgentOptions.system_prompt - Structured output returned in ResultMessage.structured_output (already dict, not JSON) - SDK handles retries autonomously via max_turns (no manual retry logic needed)
Classes¶
ClaudeSDKLLMAdapter
¶
LLM adapter using Claude Agent SDK's query() function.
This adapter implements the LLMPort Protocol for simple LLM invocation without agent loops or tool calling. Uses the SDK's query() function which is simpler than ClaudeSDKClient.
The adapter handles: - Message conversion from unified Message to prompt string - System prompt extraction to ClaudeAgentOptions.system_prompt - Usage metadata extraction from ResultMessage - Sync/async invocation with proper event loop handling - Structured output via with_structured_output()
Note: The SDK handles retries autonomously via max_turns, so no manual retry logic is needed in this adapter.
Example
from karenina.schemas.config import ModelConfig config = ModelConfig( ... id="claude-sonnet", ... model_name="claude-sonnet-4-20250514", ... model_provider="anthropic", ... interface="claude_agent_sdk" ... ) adapter = ClaudeSDKLLMAdapter(config) response = await adapter.ainvoke([Message.user("Hello!")]) print(response.content) 'Hello! How can I help you today?'
With structured output¶
class Answer(BaseModel): ... value: str structured = adapter.with_structured_output(Answer) response = await structured.ainvoke([Message.user("What is 2+2?")])
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
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 | |
Attributes¶
capabilities
property
¶
capabilities: PortCapabilities
Declare what prompt features this adapter supports.
Returns:
| Type | Description |
|---|---|
PortCapabilities
|
PortCapabilities with system prompt, structured output, and streaming support. |
Functions¶
__init__
¶
__init__(
model_config: ModelConfig,
*,
_structured_schema: type[BaseModel] | None = None,
_max_turns: int | None = None,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_config
¶ |
ModelConfig
|
Configuration specifying model, provider, and interface. |
required |
_structured_schema
¶ |
type[BaseModel] | None
|
Internal - schema for structured output mode. |
None
|
_max_turns
¶ |
int | None
|
Internal - max turns for SDK (handles retries autonomously). |
None
|
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
aclose
async
¶
Close underlying resources.
The Claude SDK adapter uses query() which doesn't hold persistent connections, so this is a no-op. Provided for interface consistency with other adapters that do require cleanup.
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
ainvoke
async
¶
ainvoke(messages: list[Message]) -> LLMResponse
Invoke the LLM asynchronously.
When ModelConfig.request_timeout is set, the whole query() drain is wrapped in asyncio.wait_for so the call has a hard wall-clock bound. The guard bounds only the wall clock seen by the caller: the SDK subprocess behind query() may linger until garbage collection after the timeout fires. A fired timeout raises a stock asyncio.TimeoutError (the builtin TimeoutError), which the ErrorRegistry classifies as TIMEOUT, matching what the langchain adapter surfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
¶ |
list[Message]
|
List of unified Message objects. |
required |
Returns:
| Type | Description |
|---|---|
LLMResponse
|
LLMResponse with content, usage metadata, and raw response. |
Raises:
| Type | Description |
|---|---|
PortError
|
If the invocation fails. |
TimeoutError
|
If the call exceeds ModelConfig.request_timeout. |
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
astream
async
¶
astream(
messages: list[Message],
) -> AsyncIterator[StreamingLLMResponse]
Stream LLM response via Claude Agent SDK.
Uses query() with include_partial_messages=True to receive AssistantMessage objects as the response is built. Text deltas are computed by tracking previously seen content.
Usage capture: partial AssistantMessages do NOT carry usage in the
SDK, the authoritative usage arrives only on the final
ResultMessage. With include_partial_messages=True the SDK also
exposes raw StreamEvent payloads, whose message_start and
message_delta events DO carry usage, so this method captures
those inline as they arrive. A mid-stream interruption therefore
still leaves whatever usage was streamed before the cut, and the
ResultMessage overrides the inline snapshot on the success path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
¶ |
list[Message]
|
List of unified Message objects. |
required |
Yields:
| Type | Description |
|---|---|
AsyncIterator[StreamingLLMResponse]
|
StreamingLLMResponse that can be iterated for text chunks. |
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
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 | |
invoke
¶
invoke(messages: list[Message]) -> LLMResponse
Invoke the LLM synchronously.
This is a convenience wrapper around ainvoke() for sync code. Always uses a dedicated thread with asyncio.run() to give the Claude Agent SDK a fresh event loop, avoiding cancel scope conflicts with BlockingPortal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
¶ |
list[Message]
|
List of unified Message objects. |
required |
Returns:
| Type | Description |
|---|---|
LLMResponse
|
LLMResponse with content, usage metadata, and raw response. |
Raises:
| Type | Description |
|---|---|
PortError
|
If the invocation fails. |
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
stream_invoke
¶
stream_invoke(
messages: list[Message], timeout: float | None = None
) -> LLMResponse
Stream with wall-clock timeout synchronously.
Always uses a dedicated thread with asyncio.run() to give the Claude Agent SDK a fresh event loop, avoiding cancel scope conflicts with BlockingPortal. Retries via RetryExecutor on transient errors (including StreamingTimeoutError with zero content, classified as RATE_LIMIT for queue congestion).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
¶ |
list[Message]
|
List of unified Message objects. |
required |
timeout
¶ |
float | None
|
Wall-clock timeout in seconds. None means no timeout. |
None
|
Returns:
| Type | Description |
|---|---|
LLMResponse
|
LLMResponse with accumulated content. |
Raises:
| Type | Description |
|---|---|
StreamingTimeoutError
|
If retries are exhausted and the stream still exceeds the wall-clock timeout. |
Source code in src/karenina/adapters/claude_agent_sdk/llm.py
with_structured_output
¶
with_structured_output(
schema: type[BaseModel],
*,
max_turns: int | None = None,
max_retries: int | None = None,
) -> ClaudeSDKLLMAdapter
Return a new adapter configured for structured output.
The returned adapter uses SDK's output_format option to constrain the LLM's output to match the provided JSON schema.
The SDK handles retries autonomously via max_turns, so no manual retry logic is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
¶ |
type[BaseModel]
|
A Pydantic model class defining the output structure. |
required |
max_turns
¶ |
int | None
|
Maximum turns for SDK (handles retries autonomously). Default is 2 (minimum needed for structured output). |
None
|
max_retries
¶ |
int | None
|
Not supported by this adapter. A warning is emitted if a non-None value is provided. The SDK handles retries autonomously via max_turns. See karenina.ports.llm.LLMPort.with_structured_output for the cross-adapter contract (LangChain and Claude Tool respect max_retries, this adapter and Deep Agents warn and ignore). |
None
|
Returns:
| Type | Description |
|---|---|
ClaudeSDKLLMAdapter
|
A new ClaudeSDKLLMAdapter instance configured for structured output. |
ClaudeSDKLLMAdapter
|
The returned adapter guarantees that response.raw will be an instance |
ClaudeSDKLLMAdapter
|
of the provided schema. |
Example
class Answer(BaseModel): ... value: str ... confidence: float structured = adapter.with_structured_output(Answer) response = await structured.ainvoke(messages) assert isinstance(response.raw, Answer)