Other framework examples for agent optimization

This topic has code examples

Claude Agent SDK

Here is a complete example using the Claude Agent SDK:

Python
1from ldai import LDAIClient
2from ldai_optimization import (
3 OptimizationResponse,
4 LLMCallConfig,
5 LLMCallContext,
6 OptimizationClient,
7 OptimizationFromConfigOptions
8)
9
10from ldai.tracker import TokenUsage
11
12from claude_agent_sdk import query, ClaudeAgentOptions
13from claude_agent_sdk.types import ResultMessage
14
15async def run_claude_optimization(optimization_key: str, ld_ai_client: LDAIClient):
16 async def handle_agent_call(
17 key: str,
18 config: LLMCallConfig,
19 context: LLMCallContext,
20 is_evaluation: bool = False,
21 ) -> OptimizationResponse:
22 model = config.model.name if config.model else "claude-opus-4-5-20251101"
23 final_message = None
24 async for message in query(
25 prompt=context.user_input or "",
26 options=ClaudeAgentOptions(
27 system_prompt=config.instructions or "",
28 model=model,
29 ),
30 ):
31 final_message = message
32
33 if not isinstance(final_message, ResultMessage):
34 raise ValueError(f"Unexpected final message type: {type(final_message)}")
35
36 u = final_message.usage or {}
37 input_tokens = u.get("input_tokens", 0)
38 output_tokens = u.get("output_tokens", 0)
39
40 return OptimizationResponse(
41 output=final_message.result or "",
42 usage=TokenUsage(
43 total=input_tokens + output_tokens,
44 input=input_tokens,
45 output=output_tokens,
46 ),
47 )
48
49 options = OptimizationFromConfigOptions(
50 project_key="default",
51 handle_agent_call=handle_agent_call,
52 handle_judge_call=handle_agent_call,
53 )
54
55 client = OptimizationClient(ld_ai_client)
56 result = await client.optimize_from_config(optimization_key, options)
57
58 return result

OpenAI Agents SDK

Here is a complete example using the OpenAI Agents SDK:

Python
1from ldai import LDAIClient
2from ldai_optimization import (
3 OptimizationResponse,
4 LLMCallConfig,
5 LLMCallContext,
6 OptimizationClient,
7 OptimizationFromConfigOptions
8)
9
10from agents import Agent
11from agents.run import Runner
12
13from ldai.tracker import TokenUsage
14
15async def run_openai_optimization(optimization_key: str, ld_ai_client: LDAIClient):
16 async def handle_agent_call(
17 key: str,
18 config: LLMCallConfig,
19 context: LLMCallContext,
20 is_evaluation: bool = False,
21 ) -> OptimizationResponse:
22 model = config.model.get_parameter("name") if config.model else "gpt-5"
23 root = Agent(
24 name=key,
25 instructions=config.instructions,
26 handoffs=[],
27 tools=[],
28 model=model,
29 )
30 response = await Runner.run(root, context.user_input or "")
31 u = response.context_wrapper.usage
32 return OptimizationResponse(
33 output=response.final_output,
34 usage=TokenUsage(
35 total=u.total_tokens, input=u.input_tokens, output=u.output_tokens
36 ),
37 )
38
39 client = OptimizationClient(ld_ai_client)
40
41 options = OptimizationFromConfigOptions(
42 project_key="default",
43 handle_agent_call=handle_agent_call,
44 handle_judge_call=handle_agent_call,
45 )
46
47 result = await client.optimize_from_config(optimization_key, options)
48
49 return result

LangChain create_agent

Here is a complete example using LangChain’s create_agent:

Python
1from ldai_optimization import (
2 OptimizationResponse,
3 LLMCallConfig,
4 LLMCallContext,
5 OptimizationClient,
6 OptimizationFromConfigOptions
7)
8
9from ldai.tracker import TokenUsage
10
11from ldai import LDAIClient
12
13from langchain.agents import create_agent
14from langchain.messages import HumanMessage
15
16async def run_langgraph_optimization(optimization_key: str, ld_ai_client: LDAIClient):
17 async def handle_agent_call(
18 key: str,
19 config: LLMCallConfig,
20 context: LLMCallContext,
21 is_evaluation: bool = False,
22 ) -> OptimizationResponse:
23 model = config.model.get_parameter("name") if config.model else "openai:gpt-5"
24
25 agent = create_agent(
26 model=model,
27 system_prompt=config.instructions,
28 )
29
30 response = agent.invoke(
31 { "messages" : [HumanMessage(context.user_input or "Complete the request")] }
32 )
33
34 last_message = response['messages'][-1]
35 u = last_message.usage_metadata
36 return OptimizationResponse(
37 output=last_message.content,
38 usage=TokenUsage(
39 total=u["total_tokens"], input=u["input_tokens"], output=u["output_tokens"]
40 ),
41 )
42
43 options = OptimizationFromConfigOptions(
44 project_key="default",
45 handle_agent_call=handle_agent_call,
46 handle_judge_call=handle_agent_call,
47 )
48
49 client = OptimizationClient(ld_ai_client)
50 result = await client.optimize_from_config(optimization_key, options)
51
52 return result

Strands

Here is a complete example using Strands:

Python
1from ldai_optimization import (
2 OptimizationResponse,
3 LLMCallConfig,
4 LLMCallContext,
5 OptimizationClient,
6 OptimizationFromConfigOptions
7)
8
9from ldai.tracker import TokenUsage
10
11from ldai import LDAIClient
12
13from strands import Agent
14from strands.models.openai import OpenAIModel
15
16async def run_strands_optimization(optimization_key: str, ld_ai_client: LDAIClient):
17 async def handle_agent_call(
18 key: str,
19 config: LLMCallConfig,
20 context: LLMCallContext,
21 is_evaluation: bool = False,
22 ) -> OptimizationResponse:
23 model = config.model.get_parameter("name") if config.model else "gpt-5"
24 params = config.model.get_parameter("params") if config.model else {}
25
26 openai_connector = OpenAIModel(
27 model_id=model,
28 params=params if params else {}
29 )
30
31 agent = Agent(system_prompt=config.instructions, model=openai_connector, callback_handler=None)
32
33 response = agent(context.user_input)
34
35 u = response.metrics.get_summary()["accumulated_usage"]
36
37 return OptimizationResponse(
38 output=str(response),
39 usage=TokenUsage(
40 total=u["totalTokens"], input=u["inputTokens"], output=u["outputTokens"]
41 ),
42 )
43
44 options = OptimizationFromConfigOptions(
45 project_key="default",
46 handle_agent_call=handle_agent_call,
47 handle_judge_call=handle_agent_call,
48 )
49
50 client = OptimizationClient(ld_ai_client)
51 result = await client.optimize_from_config(optimization_key, options)
52
53 return result