3.5 KiB
Experiment Log: Asynchronous API Calls for Performance Enhancement
Date Started: 2025-04-29
Owner: Cascade
Goal:
Improve the runtime performance of the Diplomacy game simulation (lm_game.py) by converting blocking LLM API calls to non-blocking asynchronous operations using asyncio and asynchronous client libraries. This aims to reduce the wall-clock time spent waiting for network I/O during phases involving multiple LLM interactions (initialization, planning, negotiation, order generation, state updates).
Hypothesis:
Replacing synchronous API calls managed by ThreadPoolExecutor with native asyncio operations will lead to significantly faster phase completion times, especially for negotiation and order generation where multiple calls happen concurrently.
Key Implementation Details:
- Use
asynciolibrary for managing asynchronous tasks. - Replace synchronous LLM client libraries (e.g.,
openai,anthropic) with their asynchronous counterparts (e.g.,openai.AsyncOpenAI,anthropic.AsyncAnthropic). - Refactor client methods (
generate_response,get_orders,get_conversation_reply, etc.) to beasync defand useawait. - Refactor calling functions in
agent.py,negotiations.py,planning.py, andlm_game.pyto useasync defandawait. - Replace
concurrent.futures.ThreadPoolExecutorwithasyncio.gatherfor managing concurrent async tasks. - Run the main simulation loop within
asyncio.run(). - Maintain existing logging and error handling.
Phased Implementation Plan:
- Agent Initialization: Convert
agent.initialize_agent_stateand related client calls to async. Updatelm_game.pyto run initializations concurrently withasyncio.gather. - Negotiation: Convert
negotiations.conduct_negotiationsandclient.get_conversation_replyto async. - Order Generation: Convert
client.get_orderscall chain to async. - Planning: Convert
planning.planning_phasecall chain to async. - State Update: Convert
agent.analyze_phase_and_update_statecall chain to async.
Success Metric:
Significant reduction (e.g., >30%) in total simulation runtime (total_time logged at the end of lm_game.py) for a standard game configuration (e.g., --max_year 1902 --num_negotiation_rounds 2). Compare before/after timings.
Rollback Plan: Revert changes using Git version control if significant issues arise or performance does not improve as expected.
Debugging & Results Table
| Phase Implemented | Status | Notes | Wager Outcome |
|---|---|---|---|
| 1. Agent Initialization | In Progress | Starting refactor of clients, agent init, and main loop concurrency. | -$100 |
| 2. Negotiation | Pending | ||
| 3. Order Generation | Pending | ||
| 4. Planning | Pending | ||
| 5. State Update | Pending | ||
| Overall Result | TBD | Did total runtime decrease significantly? | +$500/-$100 |