Other framework examples for agent optimization
This topic has code examples
Claude Agent SDK
Click to expand the Claude Agent SDK example
Here is a complete example using the Claude Agent SDK:
Python
from ldai import LDAIClientfrom ldai_optimization import (OptimizationResponse,LLMCallConfig,LLMCallContext,OptimizationClient,OptimizationFromConfigOptions)from ldai.tracker import TokenUsagefrom claude_agent_sdk import query, ClaudeAgentOptionsfrom claude_agent_sdk.types import ResultMessageasync def run_claude_optimization(optimization_key: str, ld_ai_client: LDAIClient):async def handle_agent_call(key: str,config: LLMCallConfig,context: LLMCallContext,is_evaluation: bool = False,) -> OptimizationResponse:model = config.model.name if config.model else "claude-opus-4-5-20251101"final_message = Noneasync for message in query(prompt=context.user_input or "",options=ClaudeAgentOptions(system_prompt=config.instructions or "",model=model,),):final_message = messageif not isinstance(final_message, ResultMessage):raise ValueError(f"Unexpected final message type: {type(final_message)}")u = final_message.usage or {}input_tokens = u.get("input_tokens", 0)output_tokens = u.get("output_tokens", 0)return OptimizationResponse(output=final_message.result or "",usage=TokenUsage(total=input_tokens + output_tokens,input=input_tokens,output=output_tokens,),)options = OptimizationFromConfigOptions(project_key="default",handle_agent_call=handle_agent_call,handle_judge_call=handle_agent_call,)client = OptimizationClient(ld_ai_client)result = await client.optimize_from_config(optimization_key, options)return result
OpenAI Agents SDK
Click to expand the OpenAI Agents SDK example
Here is a complete example using the OpenAI Agents SDK:
Python
from ldai import LDAIClientfrom ldai_optimization import (OptimizationResponse,LLMCallConfig,LLMCallContext,OptimizationClient,OptimizationFromConfigOptions)from agents import Agentfrom agents.run import Runnerfrom ldai.tracker import TokenUsageasync def run_openai_optimization(optimization_key: str, ld_ai_client: LDAIClient):async def handle_agent_call(key: str,config: LLMCallConfig,context: LLMCallContext,is_evaluation: bool = False,) -> OptimizationResponse:model = config.model.get_parameter("name") if config.model else "gpt-5"root = Agent(name=key,instructions=config.instructions,handoffs=[],tools=[],model=model,)response = await Runner.run(root, context.user_input or "")u = response.context_wrapper.usagereturn OptimizationResponse(output=response.final_output,usage=TokenUsage(total=u.total_tokens, input=u.input_tokens, output=u.output_tokens),)client = OptimizationClient(ld_ai_client)options = OptimizationFromConfigOptions(project_key="default",handle_agent_call=handle_agent_call,handle_judge_call=handle_agent_call,)result = await client.optimize_from_config(optimization_key, options)return result
LangChain create_agent
Click to expand the LangChain create_agent example
Click to expand the LangChain create_agent example
Here is a complete example using LangChain’s create_agent:
Python
from ldai_optimization import (OptimizationResponse,LLMCallConfig,LLMCallContext,OptimizationClient,OptimizationFromConfigOptions)from ldai.tracker import TokenUsagefrom ldai import LDAIClientfrom langchain.agents import create_agentfrom langchain.messages import HumanMessageasync def run_langgraph_optimization(optimization_key: str, ld_ai_client: LDAIClient):async def handle_agent_call(key: str,config: LLMCallConfig,context: LLMCallContext,is_evaluation: bool = False,) -> OptimizationResponse:model = config.model.get_parameter("name") if config.model else "openai:gpt-5"agent = create_agent(model=model,system_prompt=config.instructions,)response = agent.invoke({ "messages" : [HumanMessage(context.user_input or "Complete the request")] })last_message = response['messages'][-1]u = last_message.usage_metadatareturn OptimizationResponse(output=last_message.content,usage=TokenUsage(total=u["total_tokens"], input=u["input_tokens"], output=u["output_tokens"]),)options = OptimizationFromConfigOptions(project_key="default",handle_agent_call=handle_agent_call,handle_judge_call=handle_agent_call,)client = OptimizationClient(ld_ai_client)result = await client.optimize_from_config(optimization_key, options)return result
Strands
Click to expand the Strands example
Here is a complete example using Strands:
Python
from ldai_optimization import (OptimizationResponse,LLMCallConfig,LLMCallContext,OptimizationClient,OptimizationFromConfigOptions)from ldai.tracker import TokenUsagefrom ldai import LDAIClientfrom strands import Agentfrom strands.models.openai import OpenAIModelasync def run_strands_optimization(optimization_key: str, ld_ai_client: LDAIClient):async def handle_agent_call(key: str,config: LLMCallConfig,context: LLMCallContext,is_evaluation: bool = False,) -> OptimizationResponse:model = config.model.get_parameter("name") if config.model else "gpt-5"params = config.model.get_parameter("params") if config.model else {}openai_connector = OpenAIModel(model_id=model,params=params if params else {})agent = Agent(system_prompt=config.instructions, model=openai_connector, callback_handler=None)response = agent(context.user_input)u = response.metrics.get_summary()["accumulated_usage"]return OptimizationResponse(output=str(response),usage=TokenUsage(total=u["totalTokens"], input=u["inputTokens"], output=u["outputTokens"]),)options = OptimizationFromConfigOptions(project_key="default",handle_agent_call=handle_agent_call,handle_judge_call=handle_agent_call,)client = OptimizationClient(ld_ai_client)result = await client.optimize_from_config(optimization_key, options)return result